PHP将文件从浏览器上传到FTP [英] PHP uploading file from browser to FTP
问题描述
我想将文件从用户的计算机上载到FTP中的文件夹uploads
,它不起作用.你能帮我吗?
I want to upload files from user's machine to folder uploads
in my FTP and it doesn't work. Can you help me?
$ftp_server = "some ip";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "some name", "some password");
$target_dir = "uploads/";
$target_file = basename($_FILES["filename"]["name"]);
if (ftp_fput($ftp_conn, $target_dir.$target_file, FTP_ASCII))
{
echo "Successfully uploaded $target_file.";
}
else
{
echo "Error uploading $target_file.";
}
推荐答案
您需要指定要上传到FTP服务器的本地文件(从Web服务器开始).
You need to specify what local file (as of the webserver) you want to upload to the FTP server.
您可以使用 .阅读有关PHP中 POST方法上传的信息.
You can retrieve a name of a temporary file that contains a file uploaded via HTTP POST from user's machine to your webserver using $_FILES["filename"]["tmp_name"]
. Read about POST method uploads in PHP.
然后您可以将其传递给 ftp_put
(无需ftp_fput
):
You can then pass that to ftp_put
(no need for ftp_fput
):
ftp_put($ftp_conn, $target_dir.$target_file, $_FILES["filename"]["tmp_name"], FTP_IMAGE)
您的代码中还有两个其他问题(不是您遇到的直接问题,但是您将在解决后立即面对它们):
Two other issues in your code (which are not your immediate problems, but you will face them right after you resolve it):
-
如果要上传二进制文件(例如
.mp3
),则绝对不要使用FTP_ASCII
.使用FTP_IMAGE
.
Absolutely do not use
FTP_ASCII
, if you are uploading binary files, like.mp3
. UseFTP_IMAGE
.
您需要使用ftp_pasv
.
这篇关于PHP将文件从浏览器上传到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!