1

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.

5
  • 1
    This is probably because php is being run under a different user on the system, and that that user does not have the rights to do that action on those files. You could look into sudo to perform this Commented Aug 27, 2013 at 18:15
  • Wouldn't I get a status code of 0 then? Commented Aug 27, 2013 at 18:16
  • Last time I had this error, I didn't think of looking at the status code so I can't be sure of that, sorry. But using sudo did fix my problems Commented Aug 27, 2013 at 18:32
  • 1
    I bet PHP doesn't have permissions to either copy the file from source, or it doesn't have permissions to copy to the destination. Or it could be both. Commented Aug 27, 2013 at 18:33
  • 2
    Try to check is_writable($dest). Chances are it's false, which means, as others have said, the PHP user cannot write to it. Usually PHP is ran as the apache (www-data) or nobody user. Run echo exec("whoami"); to see the user it's running as. Commented Aug 27, 2013 at 18:44

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.