【问题标题】:With SSH2 and phpseclib, how do I upload a file to Amazon EC2 server in PHP?使用 SSH2 和 phpseclib,如何使用 PHP 将文件上传到 Amazon EC2 服务器?
【发布时间】:2020-03-06 05:55:40
【问题描述】:

我正在尝试创建一个 Web 界面,用户可以在其中上传图像文件,该文件将被发送到我的 Amazon EC2 服务器。有人告诉我为此使用 phpseclib 而不是 PHP SDK,我尝试了以下代码:

testform.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Upload Files</title>
  </head>

  <body>
    <form action="test.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="image" />
        <input type="submit" value="Upload Image" name="submit" />
    </form>
  </body>
</html>

test.php

<?php

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) 
{
    include('Net/SSH2.php');
    include('Crypt/RSA.php');

    $rsa = new Crypt_RSA();
    $rsa->loadKey(file_get_contents('KeyKey.ppk'));

    $ssh = new Net_SSH2('domain-name-of-my-server');
    if (!$ssh->login('ec2-user', $rsa)) {
        exit('Login Failed');
    }

    $target_dir = "/home/ec2-user/Test1";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $uploadOK = 1;

    strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    $image=$_FILES['image']['name'];
    $imageArr=explode('.',$image); //first index is file name and second index file type
    $rand=rand(10000,99999);
    $newImageName=$imageArr[0].$rand.'.'.$imageArr[1];

    $uploadPath = "/home/ec2-user/Test1".$newImageName;

    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) 
    {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } 
    else 
    {
        echo "File is not an image.";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) 
    {
        echo("Sorry, your file was not uploaded.");
    } 
    // if everything is ok, try to upload file
    else 
    {
        if (ssh2_scp_send($ssh, $_FILES["image"]["tmp_name"], $uploadPath, 0644)) {
            echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
        } 
        else 
        {
            echo "Sorry, there was an error uploading your file.";
        }
    }


    echo $ssh->exec('pwd');
    echo $ssh->exec('ls -la');
}
?>

到我的服务器的连接看起来很好,但是这条线

第 50 行:如果 (ssh2_scp_send($ssh, $_FILES["image"]["tmp_name"], $上传路径, 0644)

给我这个错误:

致命错误:未捕获错误:调用未定义函数 C:\xampp\htdocs\test\test.php:50 中的 ssh2_scp_send() 堆栈跟踪:#0 {main} 在 C:\xampp\htdocs\test\test.php 第 50 行抛出

我发现我提供给 ssh2_scp_send() 函数的路径有问题,我不知道如何修复它。

我在这里查看过相同问题的线程(例如Upload file to Amazon EC2 server from website by PHP),但它们主要使用 PHP SDK。

有人可以提供一些指导吗?

【问题讨论】:

    标签: php amazon-web-services amazon-ec2 webserver ssh2


    【解决方案1】:

    错误清楚地表明您正在调用undefined function

    您是否在实例上安装了所有必要的软件包和扩展?

    yum install gcc php-devel php-pear libssh2 libssh2-devel make
    

    【讨论】:

    • 我是否将它安装在我的远程服务器上?有没有办法检查我的依赖项是否安装正确?我试图在我的实例上安装它,错误似乎是说我已经安装了它们。
    【解决方案2】:

    知道你为什么要尝试使用 phpseclib libssh2。

    不管怎样,试试这个:

    #
    #-----[ FIND ]------------------------------------------
    #
        $ssh = new Net_SSH2('domain-name-of-my-server');
    #
    #-----[ REPLACE WITH ]----------------------------------
    #
        $ssh = new Net_SFTP('domain-name-of-my-server');
    #
    #-----[ FIND ]------------------------------------------
    #
            if (ssh2_scp_send($ssh, $_FILES["image"]["tmp_name"], $uploadPath, 0644)) {
    #
    #-----[ REPLACE WITH ]----------------------------------
    #
            if ($ssh->put($uploadPath, $_FILES["image"]["tmp_name"], NET_SFTP_LOCAL_FILE) {
                $sftp->chmod(0644, $uploadPath);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多