使用 PHP 将单个图像文件上传到 FTP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18718321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload single image file to FTP using PHP
提问by Elliot Reed
Pulling my hair out a bit here. All I want to do is have a HTML form and then PHP to upload the selected file to a certain directory on an FTP server, but nothing seems to be working correctly.
在这里把我的头发拉出来一点。我想要做的就是有一个 HTML 表单,然后 PHP 将选定的文件上传到 FTP 服务器上的某个目录,但似乎没有任何工作正常。
Here's the html form :
这是 html 表单:
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
And here's the PHP below it (on the same file):
这是它下面的 PHP(在同一个文件中):
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "/public_html/my/directory/";
$source_file = $_POST['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
It seems to connect to the FTP ok, but doesn't upload - failed. I'm assuming it's something to do with the way I'm handling the file to be uploaded..?
似乎可以正常连接到 FTP,但无法上传 - 失败。我假设这与我处理要上传的文件的方式有关..?
Also, while I'm here, how do I set the name of the file that's been uploaded as a variable?
另外,当我在这里时,如何将已上传的文件的名称设置为变量?
回答by jaydeep namera
Change your code here
在此处更改您的代码
$source_file = $_FILES['file']['tmp_name'];
Here i have changed $_POST to $_FILES....
在这里,我已将 $_POST 更改为 $_FILES ....
回答by Vinil Lakkavatri
It seems fine with above code, but FTP passive command i.e ftp_pasv($conn_id, true); should be after ftp_login or else it won't work. You may get this kind of errors: ftp_put(): I won't open a connection to 172.xx.xx.xx (only to xx.xxx.xxx.xxx)
上面的代码看起来没问题,但是 FTP 被动命令,即 ftp_pasv($conn_id, true); 应该在 ftp_login 之后,否则它将无法工作。您可能会收到此类错误: ftp_put(): I won't open a connection to 172.xx.xx.xx (only to xx.xxx.xxx.xxx)
<?php
$ftp_server = "your server or host";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$ftp_port = "port";
$destination_file = "/public_html/my/directory/";
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// ftp passive cmd
ftp_pasv($conn_id, true);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>