Using Linux/PHP 4.3.2 CLI:
[PHP]$fileID = fopen('myfile.txt', 'r'); // WORKS JUST FINE [/PHP]
Using Linux/PHP 4.3.8 CLI:
[PHP]$fileID = fopen('myfile.txt'); // THROWS WARNING 'no such file or
directory "myfile.txt" [/PHP]
Using Windows XP/PHP 5.0.4 CLI:
[PHP]$fileID = fopen('myfile.txt'); // THROWS WARNING 'no such file or
directory "myfile.txt"[/PHP]
In all three cases this script, let's call it myscript.php, resides in
the exact same directory as "myfile.txt". Both scripts have
permissions of 0644, same owner, same group (Unix).
For the Windows problem I managed to use a coding solution that will
use the absolute path that is generated within $_SERVER['PHP_SELF']
(which apparently only occurs within Windows - have never seen that
variable populated when using a UNIX-based OS):
[PHP]
if ($_ENV['windir'] || $_SERVER['windir']) {
$path = substr($_SERVER['PHP_SELF'], 0,
strrpos($_SERVER['PHP_SELF'], '/') + 1);
} else {
$path = '';
}
[/PHP]
This, however, will produce no absolute path information for UNIX-based
OS using "myscript.php" to access "myfile.txt". So using older
versions of PHP as recent as PHP 4.3.8 within UNIX (Linux), "$fileID =
fopen('myfile.txt', 'r');" you have to extract from $argv[0], provided
you are using CLI PHP and you just happen to NOT be in the directory
where "myscript.php" is housed, otherwise, you obviously get no
absolute path info to locate "myfile.txt", and yes, when tested in PHP
4.3.11 and PHP 5.0.4, I constantly got the warning "No such file or
directory: "myfile.txt"" even though it's clearly there with
myscript.php.
Bottom line: Is there a foolproof way of always obtaining the absolute
path of something using CLI PHP; or... what other alternatives have you
all come up with using CLI PHP to always reliably locate a file that
happens to be in the same directory as your script?
Thanx
Phil