PDA

View Full Version : Path management in php



ironikart
January 15th, 2006, 05:27 PM
I'm interested to see how other people do this, since it's something that I don't often have to manage myself, but on my current project appears to be pretty important.

I want to be able to use the full path to files on the file system in my code, but also allow it to be cross platform (ie on windows and *nix machines). I normally use:



$pathToFile = dirname(__FILE__) . '/'

echo $pathToFile;

// Linux output
# /home/site/dir/

//Windows output
# C:\home\site\dir/


but of course, that will cause problems on windows machines that use the backslash to seperate directories. Anyone have a solution for managing this (other than using relative directories!)

ironikart
January 18th, 2006, 04:53 PM
* bump

No one has done anything to manage absolute paths cross platform?

hl
January 18th, 2006, 05:40 PM
you can do an is_dir("./") and if it isn't, have it be a \.

i'm not quite sure if that'd work... but its worth a shot.

ironikart
January 18th, 2006, 08:43 PM
I've actually found today that paths like "C:\dir\to\www/more/dir" work in php when including files. I'm not sure about fopen(), fwrite() etc yet, but I was quite surprised when my code didn't spew out errors.

It's not ideal though, but I guess if it was an ideal world we'd all be using the same operating system