0


I'm having some problems referencing a file on my website in a PHP script. Let's say the full path is

www.mydomain.com/img/peggy.jpg  

I can't use relative addressing like in

../img/peggy.jpg  

because the script is in an include file which is included in several files at different levels.

/img/peggy.jpg  

doesn't seem to work either. How do I reference to an absolute path?
TIA
Steven

4 Answers 4

1

You could define(BASE_PATH, dirname(__FILE__)) in your index.php.
Then use $fileName = BASE_PATH . "/img/peggy.jpg";

Sign up to request clarification or add additional context in comments.

Comments

0

You would have to use dirname(__FILE__) . "/img/peggy.jpg".

You can use multiple, as well: dirname(dirname(__FILE__)) ...

3 Comments

This (the 2nd form) gives the full path as in http://mydomain.com/img/peggy.jpg, but it doesn't seem to be in the form expected by file_exists; it reports that the file doesn't exist, while it does. Thanks for the reply anyway.
file_exists takes a relative path, not in the form of a URL. Using file_exists(dirname(__FILE__) . "/img/peggy.jpg") will return TRUE if the file exists and is not inaccessible.
For example, skydrive.provanix.com (one of my subdomains), shows the path of the page index.php (the page that you see). The last line shows that index.php exists. The folder htdocs is my site root, and _skydrive is my subdomain folder.
0

$_SERVER['DOCUMENT_ROOT'] points to the absolute path of the current web root.

1 Comment

Thanks. Seemed to work until I tried a file in a subdomain, like sub.mydomain.com. Then I get as full path /home2/mydomain/public_html/sub/img/peggy.jpg. The sub shouldn't be there. Is there a way to get rid of it?
0

Solved by writing the full path explicitly:

/home2/mydomain/public_html/img/peggy.jpg  

Note to self: will have to edit this if I ever move to another hosting service. Even my current hosting service (Bluehost) may one day decide to rearrange its file structure.
Other solutions are still welcome!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.