bash 通过 SSH 执行 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9467090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:39:51  来源:igfitidea点击:

Executing PHP script via SSH

phpfilebashssh

提问by Ry-

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.

我想通过 SSH 将数据发送到我的服务器。数据是需要保存到服务器目录中的图像文件。

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. 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?

就是这样。但是我对 PHP 的了解是有限的。据我所知,PHP 通常在处理文件时使用 $_FILE 数组。如果我通过 SSH 发送文件,我需要使用它吗?

回答by che

Run the script via php command line executable:

通过 php 命令行可执行文件运行脚本:

php myscript.php image.jpg image2.jpg

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

您可以通过$argv数组获取文件名,并随心所欲地处理它们。

回答by Ry-

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.
    
  • 您是否只需要将它们回显给用户?

    echo file_get_contents('image.jpg');
    
  • 您是要检索传递给脚本的命令行变量吗?使用$argv.

    myScript.php image.jpg # image.jpg becomes $argv[1]
    
  • 您需要对图像进行处理吗?使用GD 函数

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

回答by skyronic

If you want to simply upload a bunch of files to a remote server, your best bet is to use scpcommand, 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. You have to first copy it to the remote server and then execute the PHP which uses file_get_contentsor something like that.

如果您通过 SSH 会话执行 PHP 脚本,则无法通过 SSH 发送文件。您必须首先将其复制到远程服务器,然后执行使用的 PHPfile_get_contents或类似的东西。

If you are surethat 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 curlto upload the file, like this:

如果你确定PHP一定是文件的接收者(可能你想做一些逻辑来确定文件名、路径),你必须把它作为一个网络服务器来托管,你可以curl用来上传文件,像这样:

curl -k -F [email protected] foo.php.