Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include file

Status
Not open for further replies.

pcsmurf

IS-IT--Management
Apr 26, 2002
47
0
0
GB
I am trying to have all my common files in one location called Common.

The code is:

<?php

// Include the MySQL class

include '/Common/MySQL.php';


$host='localhost'; // Hostname of MySQL server
$dbUser='kamac'; // Username for MySQL
$dbPass='websmurf'; // Password for user
$dbName='sitepoint'; // Database name

// Connect to MySQL
$db = &new MySQL($host,$dbUser,$dbPass,$dbName);
echo 'connected'
?>

When I run this I get the error:


Warning: main(/Common/MySQL.php): failed to open stream: No such file or directory in E:\IntraNetData\php\sitepointExamples\PHPAnthologyCode\PHPandMySQL\3K.php on line 5

Warning: main(): Failed opening '/Common/MySQL.php' for inclusion (include_path='.;c:\php4\pear') in E:\IntraNetData\php\sitepointExamples\PHPAnthologyCode\PHPandMySQL\3K.php on line 5

Fatal error: Cannot instantiate non-existent class: mysql in E:\IntraNetData\php\sitepointExamples\PHPAnthologyCode\PHPandMySQL\3K.php on line 15

Can anyone tell me where I am going wrong ? I can include the file on the same location as the source file without problems.

 
Have you looked at your path syntax?

Have you tried Windows syntax:
$path=&quot;c:\php\includes&quot;;

 
pcsmurf:
The problem is the beginning &quot;/&quot; in your filename.

On Win32, &quot;/&quot; will tell PHP to look at the root of the current drive letter. On *nix, this will tell PHP to look at the root of the entire filesystem.

If the filename should be relative to the directory in which the script is running, just remove the beginning &quot;/&quot;

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Sometimes with php/html just removing the slash won't work either. If removing the slash doesn't work, try preceding it with a &quot;./&quot; like so:

include ('./Common/MySQL.php');
 
Thanks for the inputs so far. None of your solutions actually work but I think it's on the right track.

2 problems:

1/ The php server is supplied by my ISP so access to php.ini and changing paths appears to be a bit of a no-no.

2/ I want to make the reference specific to the root folder so it can be found by files in any folder suing the same command.

If I change the include line to:
include '
The first 2 errors go away but the 3rd error:

Fatal error: Cannot instantiate non-existent class: mysql in E:\IntraNetData\php\sitepointExamples\PHPAnthologyCode\PHPandMySQL\3K.php on line 15

remains.

My initial thought is that the class is wrong but that can't be the case because it works if the class file is in the same folder as the source file.
 
be sure, that the file is really named &quot;Common/MySQL.php&quot; and not e.g. &quot;common/mysql.php&quot; - i.e. check the letter case ;-)
 
pcsmurf:
If the main script and the file it's including are on the same server, don't include a URL. You're going to take a performance hit because fetching a file via the web takes longer than opening a file on the filesystem. It's also putting an unnecessary load on the web server.


Assuming a base path of <webroot>/php/, where do your main script and include file reside?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
The include file resides on <webroot>/Common but I'm not talking about any specific main script file. The idea of the common folder is to hold routines that are regularly used throughout the site, for example database facilities. As a specific example one of the script files is held on <webroot>/download

AS an update I have now found a solution that works. One of my problems turns out to be the fact that my dev server is a windows box while the live server is a linux box.

On the dev server I use:

// Include the MySQL class
ini_set('include_path','include 'MySQL.php';

While on the live server I use:

// Include the MySQL class
include './Common/MySQL.php';

This all works but if there is a more efficient way I'm always glad to learn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top