BLenau
BLenau

Reputation: 95

PHP exec command works in shell and php command line but not on website

I have a problem where I am trying to copy a file that is generated on a server drive that I have mounted using the php exec command. However, the command doesn't work (although the return status is 1) when called from a web-page.

$src = "/mnt/...";
$dest = "/var/www/...";
exec("cp $src $dest");

I have tried printing out the command to make sure it is correct, and it is. I have also tried making sure the file exists before attempting to copy it and it is.

if (file_exists($src)) {
    exec("cp $src $dest");
}

Copying the command directly into the terminal works.

$ >cp /mnt/... /var/www/...

I've also tried using the php command line tool to run the exec command and that also works.

$ >php -r 'exec("cp /mnt/... /var/www/...");'

I have also tried using shell_exec as well, with the same results.

Upvotes: 1

Views: 10722

Answers (3)

Shahin ILDEREMI
Shahin ILDEREMI

Reputation: 97

Hostings usually disable the shell commands. On your local, you can edit your php.ini and add this line to disable the functions you want:

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

But on your live website, you need to contact your host provider to enable the functions that are disabled by default.

Upvotes: 1

Wayne Weibel
Wayne Weibel

Reputation: 973

I had a similar issue a while ago. It was essentially what all the commentators are saying. The user/permission of the web server are restricted, but also the shell environment that is being used and/or the PATH environment variable.

Upvotes: 0

Get Off My Lawn
Get Off My Lawn

Reputation: 36299

You can add the second parameter to help debug, $output will display what the cp command is doing, whether it be an error or not.

I would also recommend placing quotes around the files, just in case something with a space gets in there.

$src = "/mnt/...";
$dest = "/var/www/...";
exec("cp '$src' '$dest'", $output, $retrun_var);

var_dump($output, $retrun_var);

Upvotes: 0

Related Questions