[英]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 为什么您要尝试使用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.