简体   繁体   English

使用 SSH2 和 phpseclib,如何将文件上传到 PHP 中的 Amazon EC2 服务器?

[英]With SSH2 and phpseclib, how do I upload a file to Amazon EC2 server in PHP?

I am trying to create a web interface where users can upload an image file, and this file will be sent to my Amazon EC2 server.我正在尝试创建一个 web 界面,用户可以在其中上传图像文件,该文件将发送到我的 Amazon EC2 服务器。 I was told to use phpseclib instead of PHP SDK for this and I have tried the following code:有人告诉我使用 phpseclib 而不是 PHP SDK 为此,我尝试了以下代码:

testform.php测试表.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

<?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');
}
?>

The connection to my server looks fine, but this line到我的服务器的连接看起来不错,但是这条线

line 50: if (ssh2_scp_send($ssh, $_FILES["image"]["tmp_name"], $uploadPath, 0644)第 50 行:如果 (ssh2_scp_send($ssh, $_FILES["image"]["tmp_name"], $uploadPath, 0644)

is giving me this error:给我这个错误:

Fatal error: Uncaught Error: Call to undefined function ssh2_scp_send() in C:\xampp\htdocs\test\test.php:50 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test\test.php on line 50 Fatal error: Uncaught Error: Call to undefined function ssh2_scp_send() in C:\xampp\htdocs\test\test.php:50 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test\test.php在第 50 行

I figured that something is wrong with the paths that I have provided to ssh2_scp_send() function and I have no idea how to fix it.我认为我提供给 ssh2_scp_send() function 的路径有问题,我不知道如何修复它。

I have looked at threads on here with the same questions (like Upload file to Amazon EC2 server from website by PHP ), but they are mostly using PHP SDK.我在这里查看了有关相同问题的线程(例如PHP 从网站上传文件到 Amazon EC2 服务器),但它们主要使用 PHP SDK。

Would anyone please offer some guidance?有人可以提供一些指导吗?

The error clearly says that you are calling an undefined function .该错误清楚地表明您正在调用undefined function

Have you installed all necessary packages and extension on your instance?您是否在您的实例上安装了所有必要的软件包和扩展?

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

idk why you're trying to use phpseclib and libssh2. idk 为什么您要尝试使用 phpsecliblibssh2。

Anyway, try this:无论如何,试试这个:

#
#-----[ 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);

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

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