php - 使用 SSH2 和 phpseclib,如何使用 PHP 将文件上传到 Amazon EC2 服务器?
问题描述
我正在尝试创建一个 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)
给我这个错误:
致命错误:未捕获错误:调用 C:\xampp\htdocs\test\test.php:50 中未定义的函数 ssh2_scp_send() 堆栈跟踪:#0 {main} 在 C:\xampp\htdocs\test\test.php 中抛出在第 50 行
我认为我提供给 ssh2_scp_send() 函数的路径有问题,我不知道如何修复它。
我在这里查看了具有相同问题的线程(例如通过 PHP 从网站将文件上传到 Amazon EC2 服务器),但它们主要使用 PHP SDK。
有人可以提供一些指导吗?
解决方案
该错误清楚地表明您正在调用undefined function
.
您是否在实例上安装了所有必要的软件包和扩展?
yum install gcc php-devel php-pear libssh2 libssh2-devel make
推荐阅读
- spring - spring-boot 错误 org.springframework.security.core.userdetails.User 无法转换为 in.cad.security.model.MyUserPrincipal 类
- sql - 如何从文本中提取单位
- matlab - 在 Matlab 的 mvncdf 函数中定义 sigma
- swift - Swift UI facetime 链接失败(OSStatus 错误 -10814。)
- python - 使用scrapy生成项目
- c# - 如何在 c# 表单中输入 Lshift 和 Rshift?
- ibm-appid - 带有 ADFS 的 IBM APP ID 使用 Windows 集成身份验证 WIA
- android - 使用 Java 将 android studio 连接到 azure sql 数据库
- c# - 长时间运行的计算 .net 核心的性能问题 - 单独的线程池
- java - Java JTabbedPane 只看到一个选项卡