[英]php upload local file to ftp server
i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this: 我知道ftp_put方法将文件从本地服务器计算机上载到ftp服务器,但是当我尝试执行这样的简单脚本时,在使用该文件时遇到了问题:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size! 该操作成功完成,只是上传到我的ftp服务器的文件始终为零字节!
also i tried to enable passive mode but it still uploads an empty file. 我也尝试启用被动模式,但它仍然上传一个空文件。
Try enabling track_errors
and access $php_errormsg
尝试启用track_errors
并访问$php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);
I had the same issue. 我遇到过同样的问题。 When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected. 当我将“ FTP_ASCII”更改为“ FTP_BINARY”时,它解决了我的问题,并按预期上传了文件。
I had ran into this as well. 我也遇到过这个问题。 Bit late but thought I'd post my solution: 有点晚了,但以为我会发布我的解决方案:
$file_name = "localfile.txt";
Get content from your existing file 从现有文件中获取内容
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content ...或制作临时文件内容
$content = "This is content";
upload file 上传文件
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.