So I am creating a PHP script that uploads an image from my local environment to a remote server. I have my SSH keys set up on both ends and if I simply execute the scp command in terminal it sometimes does not ask for password and sometimes it does. Also, for some reason, it takes about 30-60 seconds to return a result of the scp command.

    $result = system("scp /home/pi/camImage1.jpg username@<myserver>:~/public_html/folder1/innerfolder/camImage.jpg");
    echo $result;

Whenever I use it on the php side, it always asks for password. Any suggestions on what the heck is going on?

  • take a look at PHP SSH2 library. It can make your life easier if you're trying to do SSH stuff in PHP.
    – VL-80
    Jan 4, 2014 at 20:45
  • The PHP process might be running as a different user, so it wouldn't see your usual key.
    – jjlin
    Jan 4, 2014 at 22:08

1 Answer 1


As your regular user I am going to guess you have the ssh key in menory (ssh-agent) so it does not require a password after the first connection. When running the scp command with php you did not specify a key, so my guess is you are being asked the user login password and not the ssh key.

IMO, if you wish to run this command via php, I would advise you make a key with no password and on the server disable login via password. You then specify a key with the scp command.

scp -i /home/pi/.ssh/your_key ...

for additional information on using keys, disabling password authentication, see your distro's documentation.

  • This will probably still have issues in that the www-data user doesn't have permissions to access any key the the /home/user hierarchy. It's not a good idea to do it, but the solution is likely to be creating a key under the www-data user.
    – joat
    Jan 4, 2014 at 22:42
  • Probably. You might be able to the www-data the proper ownership/group , but I am not sure.
    – Panther
    Jan 4, 2014 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.