Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gammamonk

macrumors 6502a
Original poster
Jun 4, 2004
667
108
Madison, WI
Hey everybody--

I'm trying to use php to access files in a shallower path on my webserver, and I can't get it going. To clarify:

from:
http://www.website.edu/computing/faq/1747/index.php

I need to access: /computing/footer.php

When I do,
if(file_exists("/computing/footer.php");
it doesn't work.

I can do:
if(file_exists("http://website.edu/computing/faq/1747/index.php");
but that's insane, and the footer breaks when I actually use it.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Hey everybody--

I'm trying to use php to access files in a shallower path on my webserver, and I can't get it going. To clarify:

from:
http://www.website.edu/computing/faq/1747/index.php

I need to access: /computing/footer.php

When I do,
if(file_exists("/computing/footer.php");
it doesn't work.

I can do:
if(file_exists("http://website.edu/computing/faq/1747/index.php");
but that's insane, and the footer breaks when I actually use it.

file_exists uses an OS file system path, not an absolute URL like path. In other words, the file that is served at http://website.edu/computing/footer.php will not be found on the filesystem at /computing/footer.php

That said, you have two options. Use a fully qualified file path - the path will really be based on your webserver configuration - OR simply use a relative path

In your example, if you are trying to access it on http://website.edu/computing/faq/1747/index.php then you could use

file_exists("../../footer.php")

Relative paths are easier and should you move your site to another server or directory, they will still work (for the most part)!

Hope that makes sense and Good Luck
 

gammamonk

macrumors 6502a
Original poster
Jun 4, 2004
667
108
Madison, WI
Ok cool--

so I did:

<?php if(file_exists("../../../COMPUTING/footer.php")) require("../../../COMPUTING/footer.php") ?>

And that worked. -- But I'd love to make it more solid. I need to have the exact number of "../" in there for it to work. I can't just do ../../../../../ until the cows come home, as I tried that and it didn't work. I wish I could just get to the root of the webserver and then go.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Ok cool--

so I did:

<?php if(file_exists("../../../COMPUTING/footer.php")) require("../../../COMPUTING/footer.php") ?>

And that worked. -- But I'd love to make it more solid. I need to have the exact number of "../" in there for it to work. I can't just do ../../../../../ until the cows come home, as I tried that and it didn't work. I wish I could just get to the root of the webserver and then go.

Technically, you could add the Computing folder location to the include_path of your php.ini, but my guess is you don't have control of this file.

If you really want, find out the file path of the Computing Folder on the server and store that as a constant in a constants file, then you can always use the constant to build the path you want, but the caveat is that to include the constants file, you still need a full path or relative file path.

Relative paths aren't that bad. Also, if you use include() instead of require(), it will generate a warning but continue processing, where require() will generate a FATAL error. This means you don't necessarily need the file_exists() check, because most web servers don't show warnings (again a php.ini setting)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.