Jump to content

Problems with path....


riteshn

Recommended Posts

Hi

I have an app whose file structure is like:

[code]
index.php
csp-admin/apply.php
csp-includes/db.php
[/code]

Now in csp-admin/apply.php, I want to add csp-includes/db.php so at the top, i added:

[code]
require("./csp-includes/db.php");
[/code]

and it gives me an error:

[code]
Warning: require(./csp-includes/db.php) [function.require]: failed to open stream: No such file or directory in /Users/acidity/Sites/csp/csp-admin/apply.php on line 12
[/code]

But this code in db.php works:

[code]
require('./index.php');
[/code]

I saw the docs on REQUIRE at http://us3.php.net/require and there seems to be a problem with the PATH issue. What is the problem with my code?

Ritesh

Link to comment
Share on other sites

the single dot would imply you're referring to the current directory. personally, i always steer clear of ./ and ../ when dealing with includes.

try require($_SERVER['DOCUMENT_ROOT'] . '/csp-includes/db.php') instead, and require($_SERVER['DOCUMENT_ROOT'] . '/index.php') in your db.php
Link to comment
Share on other sites

Even I was thinking of the above method but I have another peculiar problem. I am using Mac OS X with Apache that comes inbuilt in it and PHP module downloaded from: http://www.entropy.ch/software/macosx/

Now the inbuilt APACHE is configured like, if I do: http://localhost/username : it takes pages from: /Users/acidity/Sites/ so my files are at: /Users/acidity/Sites/csp/

If I do:

[code]
echo($_SERVER['DOCUMENT_ROOT'])
[/code]

I get,

[code]
/Library/WebServer/Documents/
[/code]

where as it should give probably: [code]/Users/acidity/Sites[/code]

so I use the code:

[code]require($_SERVER['DOCUMENT_ROOT'] . '/csp-includes/db.php')
[/code]

I get the error:

[code]
Fatal error: require() [function.require]: Failed opening required '/Library/WebServer/Documents/csp-includes/db.php' (include_path='.:/usr/local/php5/lib/php') in /Users/acidity/Sites/csp/csp-admin/search.php on line 14[/code]

Now, I have 0 idea of Apache, so I am in a fix :(

Ritesh
Link to comment
Share on other sites

if you have a file that you pretty much include on EVERY page in your site, you could always define the docroot with one of:
[code]
<?php
// for MacOSX
define('DOCROOT', '/Users/acidity/Sites'); //change to suit

// for other
define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
?>
[/code]
and then use
[code]
require(DOCROOT . '/csp-include/db.php');
[/code]
or similar.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.