简体   繁体   English

通过SSH执行PHP脚本

[英]Executing PHP script via SSH

I want to send data to my server via SSH. 我想通过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? 如果我通过SSH运行脚本,如何获取PHP脚本读取图像文件?

For example, if I used bash I could do 例如,如果我使用bash可以

./uploadimage.bash -image.jpg

and that would be that. 那就是那样。 But my knowledge of PHP is limited. 但是我对PHP的了解有限。 Usually PHP uses the $_FILE array when dealing with files, as far as I know. 据我所知,通常在处理文件时,PHP使用$ _FILE数组。 Do I need to use this if I send the files by SSH? 如果我通过SSH发送文件,是否需要使用它?

Run the script via php command line executable: 通过php命令行可执行文件运行脚本:

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. 您可以通过$ argv数组获取文件名,并以任意方式处理它们。

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 . 使用$argv

     myScript.php image.jpg # image.jpg becomes $argv[1] 
  • Do you need to do processing on the images? 您需要对图像进行处理吗? Use the GD functions . 使用GD功能

     $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命令而不是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. 如果通过SSH会话执行PHP脚本,则无法通过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. 您必须首先将其复制到远程服务器,然后执行使用file_get_contents类的PHP。

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: 如果您确定 PHP必须是文件的接收者(也许您想做一些逻辑以确定文件名,路径),则必须将其托管为网络服务器,并且可以使用curl来上传文件,例如这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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