簡體   English   中英

使用 SSH2 和 phpseclib,如何將文件上傳到 PHP 中的 Amazon EC2 服務器?

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

我正在嘗試創建一個 web 界面,用戶可以在其中上傳圖像文件,該文件將發送到我的 Amazon EC2 服務器。 有人告訴我使用 phpseclib 而不是 PHP SDK 為此,我嘗試了以下代碼:

測試表.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>

測試.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"], $uploadPath, 0644)

給我這個錯誤:

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 行

我認為我提供給 ssh2_scp_send() function 的路徑有問題,我不知道如何修復它。

我在這里查看了有關相同問題的線程(例如PHP 從網站上傳文件到 Amazon EC2 服務器),但它們主要使用 PHP SDK。

有人可以提供一些指導嗎?

該錯誤清楚地表明您正在調用undefined function

您是否在您的實例上安裝了所有必要的軟件包和擴展?

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

idk 為什么您要嘗試使用 phpsecliblibssh2。

無論如何,試試這個:

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