Reputation: 93
<?php
if(isset($_POST["submit"])) {
$filename = $_FILES["fileToUpload"]["name"];
require_once('/home/Script_Server/login_server_ftp.php');
$resConnection = ssh2_connect($strServer, $strServerPort);
if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)){
//Initialize SFTP subsystem
echo "connected";
$resSFTP = ssh2_sftp($resConnection);
/*la posizione resSFTP è la / assoluta nel server*/
$resFile = fopen("ssh2.sftp://{$resSFTP}/". "var/www/html/mysite.com/folder/" . $filename, 'w');
$srcFile = fopen("/var/www/html/mysite.com/folder/".$filename, 'r');
$writtenBytes = stream_copy_to_stream($srcFile, $resFile);
echo "Byte scritti: " . $writtenBytes;
fclose($resFile);
fclose($srcFile);
}else{
echo "Unable to authenticate on server";
}
} /*Chiusura POST */
?>
Good afternoon , i've a problem with this script called by the form in the html page before this , i catch the file with: $_FILES["fileToUpload"]["name"].
This not return error but it upload only the name of file with 0 Bytes size, also the number of writtenBytes is 0 , i'am using CentOs.
Thanks for help .
Upvotes: 0
Views: 1953
Reputation: 14883
After looking at this a while it's pretty obvious... PHP can't read the file you're asking it to read. I'm supprised there's no errors in the logs about this, the second fopen
will fail.
Thats because you're trying to read from the wrong filename
"/var/www/html/mysite.com/folder/" . $_FILES["fileToUpload"]["name"]
That's not where the file is stored when you upload. Assuming the field in the form was named fileToUpload
, the file uploaded by the user will be stored here:
$_FILES['userfile']['tmp_name']
That is you want this line in your code:
$srcFile = fopen($_FILES['userfile']['tmp_name'], 'r');
Upvotes: 1