Reputation: 179
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. I was told to use phpseclib instead of PHP SDK for this and I have tried the following code:
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');
}
?>
The connection to my server looks fine, but this line
line 50: if (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
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.
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.
Would anyone please offer some guidance?
Upvotes: 1
Views: 703
Reputation: 16792
idk why you're trying to use phpseclib and libssh2.
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);
Upvotes: 0
Reputation: 2157
The error clearly says that you are calling an undefined function
.
Have you installed all necessary packages and extension on your instance?
yum install gcc php-devel php-pear libssh2 libssh2-devel make
Upvotes: 0