exec() and error handling
Posted: Mon Aug 04, 2008 10:41 pm
OK -- I'm a PHP newbie and stumped by exec() error handling.
The short version is that I try to run:
and what I get is both the copied file from exec AND the die message (and halted PHP).
---------
I'm running PHP 5.2.6 under XAMPP 1.6.7 for Linux (on Fedora 9). Safe mode is off (I checked my php.ini.) PHP is running as user "nobody." When I try the command:
or, alternatively,
I always get the "Bad cp." message (and the PHP is halted, as per the die command) -- but the file testA.dat is successfully copied to testB.dat.
I get exactly the same result if I use absolute paths (for the command and for the original file and for the destination.)
If I just run the exec command, without including "or die," the command runs.
If I instead use the command
the command runs and I get a blank $output_array (since there's no output to terminal) and a $return_val of 0.
If I try setting the result in a variable instead of using die:
then $success always comes up false (but testA.dat is still copied to testB.dat.).
If I use system() or passthru() instead of exec(), again I get the same result -- die() still dies, or $success comes up false, whether or not I use absolute paths. (But the file is still copied successfully.)
For reference, user "nobody" is what Apache/PHP are running as, the owner of the base directory for the web server (where testA.dat is sitting) is "nobody" and the owner has rwx permissions for the directory, and both the owner and group of testA.dat is "nobody"; the file permissions for testA.dat are -rwxr-xr-x.
Why in the world am I apparently getting exec(cp) AND die?!
Though getting rid of the error checking ( or die() ) allows the command to work, I'd be much happier being able to actually error-check my commands instead of blithely assuming they run when I call them from PHP. And while cp is a trivial command, it is important for me to be able to call a much more complicated external program which will take an input file and from it, produce an output file.
I'm pretty flabbergasted here, and appreciate any help you can offer!
The short version is that I try to run:
Code: Select all
exec("cp testA.dat testB.dat") or die ("Bad cp.");---------
I'm running PHP 5.2.6 under XAMPP 1.6.7 for Linux (on Fedora 9). Safe mode is off (I checked my php.ini.) PHP is running as user "nobody." When I try the command:
Code: Select all
exec("cp testA.dat testB.dat") or die ("Bad cp.");Code: Select all
(exec("cp testA.dat testB.dat")) or (die ("Bad cp."));I get exactly the same result if I use absolute paths (for the command and for the original file and for the destination.)
If I just run the exec command, without including "or die," the command runs.
If I instead use the command
Code: Select all
exec("cp testA.dat testB.dat", $output_array, $return_val);If I try setting the result in a variable instead of using die:
Code: Select all
$success = exec("cp testA.dat testB.dat");If I use system() or passthru() instead of exec(), again I get the same result -- die() still dies, or $success comes up false, whether or not I use absolute paths. (But the file is still copied successfully.)
For reference, user "nobody" is what Apache/PHP are running as, the owner of the base directory for the web server (where testA.dat is sitting) is "nobody" and the owner has rwx permissions for the directory, and both the owner and group of testA.dat is "nobody"; the file permissions for testA.dat are -rwxr-xr-x.
Why in the world am I apparently getting exec(cp) AND die?!
Though getting rid of the error checking ( or die() ) allows the command to work, I'd be much happier being able to actually error-check my commands instead of blithely assuming they run when I call them from PHP. And while cp is a trivial command, it is important for me to be able to call a much more complicated external program which will take an input file and from it, produce an output file.
I'm pretty flabbergasted here, and appreciate any help you can offer!