I need to open a file that will always be in a directory, files, under the PHP scripts. This is in a Windows environment, so the backslashes need to be part of the path (I assume).
What is the best way to accomplish this?
Many thanks...
Todd
I need to open a file that will always be in a directory, files, under the PHP scripts. This is in a Windows environment, so the backslashes need to be part of the path (I assume).
What is the best way to accomplish this?
Many thanks...
Todd
PHP is usually smart enough to convert "/" to "\" when needed with regard to file-system paths. So you can do...
include '../includes/filename.php';
...and if it's running under windows it will automatically convert those slashes to back-slashes.
If you need for some other purpose to be sure about it (or just generally anal about it ), you can use the DIRECTORY_SEPARATOR pre-defined constant:
include '..'.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'filename.php';
For files to be opened with fopen(), a fully qualified path appears to be necessary. In other words, the first works; not th second:
Fname: c:\Inetpub\wwwroot\tcrc\php\files\forgot.txt
Fname2: ..\files\forgot.txt
What is the best way to get
c:\Inetpub\wwwroot\tcrc\php\ (the location of the scripts)
Todd
Either will work fine; as has been said, PHP can handle file systems just fine.
How did you use it? If you did something like:
"..\files\forgot.txt"
then no, of course it wont work; the string in the code above evaluates to something like "..ilesorgot.txt". Either one of these should work, however:
'..\files\forgot.txt'
or
"..\\files\\forgot.txt"
Am I making an error in this code?
$fname = "c:\\Inetpub\\wwwroot\\tcrc\\php\\files\\forgot.txt";
$fname2 = "..\\files\\forgot.txt";
echo "Fname: " . $fname . "<br>";
echo "Fname2: " . $fname2 . "<br>";
echo "Exist: " . file_exists($fname) . "<br>"; (Exists)
echo "Exist: " . file_exists($fname2) . "<br>"; exit; (Does not exist)
Fname: c:\Inetpub\wwwroot\tcrc\php\files\forgot.txt
Fname2: ..\files\forgot.txt
Exist: 1
Exist:
If you do:
echo getcwd();
in that same script, what does it output?
EDIT: If from this:
rtcary wrote:c:\Inetpub\wwwroot\tcrc\php\ (the location of the scripts)
you mean that the .php script that is being run resides in that directory, and you're trying to get to c:\Inetpub\wwwroot\tcrc\php\files\forgot.txt, then "..\files\forgot.txt" isn't even the correct relative path to begin with.
"..\files" means "(parent directory)\files". So in otherwords, you're trying to access c:\Inetpub\wwwroot\tcrc\files.
Well, without knowing in what directory the script is be run, there's no way to be sure if the relative path makes sense.
Yup....".\files\forgot.txt" is what I should be using (and it works).
Sorry for the inconveniences!
Todd