Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP @exec is failing silently

Tags:

php

iis

exec

This is driving me crazy. I'm trying to execute a command line statement on a windows box for my PHP web app. It's running on windows XP, IIS5.1. The web app is running fine, but I cannot get @exec() to work with a specific contactenated variable. My command construction looks like this:

$cmd = ($config->svn." cat ".$this->repConfig->svnParams().quote($path).' -r '.$rev.' > '.quote($filename));

This command does not work as is above, when it generates the following string:

svn --non-interactive --config-dir /tmp cat "file:///c:/temp/test/acccount/dbo_sproctest.sql" -r 1 > "C:\Inetpub\sites\websvn\temp\wsv5B45.tmp"

If I copy/paste this to my own command line, it works fine.

If I hard code that very same path instead of adding it with the variable, it works! I've tried with and without quotes around the file name. I've tried with and without quotes around the entire command. I've tried other directories. I've tried passing an output paramter to exec(), and it comes back empty (Array () ). I've tried redirecting the output of the error stream of the command to a file, and that error output file never gets created.

The only thing I can possibly concieve of is that exec() is failing silently. What on earth am I doing wrong here? If I hard code the file path, using the same dir structure and filename, it works fine. If I don't, it doesn't.

Maybe the slashes () in the file path aren't being escaped properly, but when I do it manually with single quotes they are not considered escape sequences??

UPDATE:

I took the @ off of exec, and still not seeing any errors.

I gave the full path to SVN, still no luck. It should be noted that the command worked fine before with the non-full path SVN so long as I manually specify the file destination for cat.

Update 2: RE: Kieth

I'm calling exec by trying both:

exec($cmd);

or

exec($cmd, $out);

My php.ini already had safe_mode = 0.

I added error_reporting(E_ALL); and didn't see anything new

If I echo (or print_r) my exec call, I am not actually seing anything

If I echo (or print_r) my exec call when included an output var, I get an empty arr

Update 3

I tried both escapeshellcmd and escapeshellarg to no avail (good idea though).

I should add that the file is being created through invoking

tempnam("temp", "wbsn");

The fact that it works just fine if I manually specify the string instead of letting it be generated by tempname seems to suggests that the source of the problem, but I can't figure out how. I did a comparison of the manual string with the one generated, and it came back as a match.

like image 269
Matt Avatar asked Nov 28 '22 02:11

Matt


2 Answers

@exec will always fail silently, because @ is PHP's error suppression operator.

like image 124
Powerlord Avatar answered Dec 01 '22 00:12

Powerlord


Not sure if this will help you since you're on Windows and I'm on Linux, but I ran into this same problem of silent errors from PHP exec(). I figured out that the command I was attempting to issue (nconvert) sends its error messages to the standard error stream, not standard out. So I added

2>&1

at the end of the command line to redirect my error back to the standard stream. Then I could see that nconvert was giving me a permission denied error.

like image 24
matt Avatar answered Nov 30 '22 22:11

matt