简体   繁体   中英

Executing PHP script via SSH

I want to send data to my server via SSH. The data is image files that need to be saved into directories on the server.

If I run the script via SSH how can I get a PHP script to read the image files?

For example, if I used bash I could do

./uploadimage.bash -image.jpg

and that would be that. But my knowledge of PHP is limited. Usually PHP uses the $_FILE array when dealing with files, as far as I know. Do I need to use this if I send the files by SSH?

Run the script via php command line executable:

php myscript.php image.jpg image2.jpg

You can the get the file names via $argv array and deal with them any way you please.

It depends on what you need to do with the images.

  • Do you just need to echo them out to the user?

     echo file_get_contents('image.jpg'); 
  • Are you meaning to retrieve the command-line variables passed to the script? Use $argv .

     myScript.php image.jpg # image.jpg becomes $argv[1] 
  • Do you need to do processing on the images? Use the GD functions .

     $image = imagecreatefromjpeg('image.jpg'); // Do processing imagejpeg($image); // Pass a filename if you want to save it instead of output it. 

If you want to simply upload a bunch of files to a remote server, your best bet is to use scp command, rather than PHP

scp /your/file.png username@host:/remote/path.png

If you execute a PHP script through an SSH Session, there's no way you can send a file through SSH. You have to first copy it to the remote server and then execute the PHP which uses file_get_contents or something like that.

If you are sure that PHP must be the recipient of the file (maybe you want to do some logic to determine the file name, path), you have to host it as a webserver, and you can use curl to upload the file, like this:

curl -k -F myfile=@image.png foo.php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM