Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Require() - working directory?
Old 02-20-2008, 05:41 AM Require() - working directory?
RadGH's Avatar
Skilled Talker

Posts: 76
Name: Radley
Trades: 0
I've noticed more than once, that when you use Require() or Include() with a file, if said file uses any require/include it will use the path of the original file.

For example...

Code:
Index.php--
<?php include "php_inc/meta.php" ?>
some code...
 some code...

php_inc/meta.php --
<?php include "globals.php" //This would be in the same folder (php_inc)// ?>
some code...
some code...

php_inc/globals.php
some code...
some code...
This gives an error, in meta.php the file "globals.php" is not found in the folder owned by "index.php". Even though its being included from a file in the folder "php_inc", it ignores that files location and uses the root folder.

This is even more frustrating now that I have it to work across every page in my Root folder, I only have to edit one file to change all files. But now if I make a subfolder, I get tons of missing require() errors, and all the images are broken links as well.



So how do I make Require() use the same directory of the file being required for its code?
RadGH is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-20-2008, 06:49 AM Re: Require() - working directory?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
http://www.webmaster-talk.com/php-fo...tml#post539759
and numerous other threads on the forum....
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-20-2008, 06:49 AM Re: Require() - working directory?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
If you run php script under apache all relative paths will be resolved from the real path of the script which was used to process the request. If you always create your website in such a way that all requests are processed either by single script or by scripts that all reside in the same directory you can use relative includes. In all other cases you will have to define different sorts of prefixes to prepend to the name of file to include.

The best and the easiest and most obvious way to avoid messing up with files not found is using absolute paths while including or requiring files.
PHP Code:
$file $_SERVER['DOCUMENT_ROOT'].'/file/to/include.php';
include(
$file); 
and no more problems. Never.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-20-2008, 06:55 AM Re: Require() - working directory?
RadGH's Avatar
Skilled Talker

Posts: 76
Name: Radley
Trades: 0
Quote:
Originally Posted by tripy View Post
Eh, sorry. I searched with Google and search PHP's website and found several things about changing include_path but that didn't seem to be my problem. It's hard to find something you expect to be so simple and common though...
Although I admit I didn't use the search feature here. Probably should have over google, since this is a support site compared to thousands of example scripts and such

Anyway, thanks mtishetsky and tripy that absolute path should work great. Haven't used global variables yet (other than _GET and _POST).


------


EDIT: Another problem. This obviously doesn't fix the images.

"C:\xampp\htdocs\images/myimg.png"

Most search results for site root and images return using a slash...

"/images/myimg.png"

However, when used from a subdomain it retains the subdomain. It doesn't seem to work as the site root.

url('/image/myimg.png'); still returns:
"/blog/images/myimg.png" (Not supposed to use blog folder)

Is there something like $_SERVER("WEBSITE_ROOT") or something that rather than returning the local address, returns the website address and/or default directory?

ex:

$_SERVER("WEBSITE_ROOT") . "/images/myimg.png"; when used in another folder would output: http://my_site.com/images/myimg.png"

Anything like this exist? And how exactly does the forward slash work? (Because it doesn't seem to do what I need it to do...)

Last edited by RadGH; 02-20-2008 at 07:37 AM..
RadGH is offline
Reply With Quote
View Public Profile
 
Old 02-20-2008, 08:06 AM Re: Require() - working directory?
Ultra Talker

Posts: 483
Trades: 0
I always prefer the dirname( __FILE__ ) way of doing things.

Instead of including via relative paths such as:
include( '../../includes.php' );

I use:
include( dirname( __FILE__ ) . '/../../includes.php' );

That way you get to type in what are essentially relative paths but the include itself works on an absolute path. I find this a bit better then using DOCUMENT_ROOT as a base because doing it that way your code still needs to assume what subdirectory (if any) the 'root' of your application is in.
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 02-20-2008, 08:24 AM Re: Require() - working directory?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
This obviously doesn't fix the images.

"C:\xampp\htdocs\images/myimg.png"
Because the images are relative to the web site, not the server filesystem.
Either use
Code:
<img src="/images/myImg.png"/>
or append the server name
PHP Code:
<img src="http://<?php echo $_SERVER['SERVER_NAME'];?>/images/myImg.png"/>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-21-2008, 01:27 AM Re: Require() - working directory?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Quote:
Originally Posted by tripy View Post
or append the server name
PHP Code:
<img src="http://<?php echo $_SERVER['SERVER_NAME'];?>/images/myImg.png"/>
Replace 'SERVER_NAME' with 'HTTP_HOST' because server name does not have to be equal to your website's hostname and in 99% cases it won't.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Require() - working directory?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25510 seconds with 12 queries