简体   繁体   中英

Upload image via SFTP put using PHP is uploading an 0 bytes file

I'm trying to upload an image to a remote SFTP server using PHP and HTML form to get the file. The file is uploaded successfully but when I check the directory, the file had 0 bytes (or just 1 byte).

I already checked the php.ini "upload_max_filesize" and is all right. I've tried a lot of codes but it's still the same. Here is that part of the code:

    include('remote_conexion.php'); //this file already has the include SFTP.php and the connection

    $foto = ($_FILES['avatar']['name']);
// Upload file
$sftp->put('/home/user/images/avatars/',$foto, NET_SFTP_LOCAL_FILE);

Here is a screenshot with the file uploaded to the server. Just 1 byte, I don't know what to do. https://prnt.sc/pj9ng1

From PHP Docs :

$_FILES['userfile']['name']
//The original name of the file on the client machine.

$_FILES['userfile']['tmp_name']
//The temporary filename of the file in which the uploaded file was stored on the 
server.

Just change "name" to "tmp_name", but after or before that you have to rename the file, because temporary filenames are ugly

I've solved the problem: I just has to add another variable with the tmp_name and add this to my sftp->put Here is the code:

$foto = ($_FILES['avatar']['name']);
$foto2 = ($_FILES['avatar']['tmp_name']);
$sftp->put('/home/natalia/images/avatars/'.$foto, $foto2 , NET_SFTP_LOCAL_FILE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM