简体   繁体   English

使用 PHP 通过 SFTP 上传图像正在上传 0 字节文件

[英]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.我正在尝试使用 PHP 和 HTML 表单将图像上传到远程 SFTP 服务器以获取文件。 The file is uploaded successfully but when I check the directory, the file had 0 bytes (or just 1 byte).该文件已成功上传,但是当我检查目录时,该文件有 0 个字节(或只有 1 个字节)。

I already checked the php.ini "upload_max_filesize" and is all right.我已经检查了 php.ini "upload_max_filesize" 并且没问题。 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.只有1个字节,我不知道该怎么办。 https://prnt.sc/pj9ng1 https://prnt.sc/pj9ng1

From PHP Docs :来自 PHP 文档

$_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只需将“name”更改为“tmp_name”,但在此之后或之前您必须重命名文件,因为临时文件名很难看

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:我已经解决了这个问题:我只需要使用 tmp_name 添加另一个变量并将其添加到我的 sftp->put 下面是代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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