php 通过PHP表单FTP上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14280688/
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
FTP upload via PHP form
提问by Raffinatore
I want to upload a file via FTP upload in a form.
我想通过 FTP 上传表单上传文件。
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Here is the PHP file:
这是PHP文件:
<?php
$ftp_server = "xxx";
$ftp_username = "xxx";
$ftp_password = "xxx";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
// login
if (@ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "conectd as $ftp_username@$ftp_server\n";
}
else
{
echo "could not connect as $ftp_username\n";
}
$file = $_FILES["file"]["name"];
$remote_file_path = "/home/www/lifestyle69/import/".$file;
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
The FTP connection connects successfully but the file is nowhere.
FTP 连接成功,但文件不存在。
Can anybody help me?
有谁能够帮助我?
Thanks!
谢谢!
采纳答案by PleaseStand
Because you have <input name="uploadedfile" type="file" />:
因为你有<input name="uploadedfile" type="file" />:
$file = $_FILES["file"]["name"]; // wrong
$file = $_FILES["uploadedfile"]["name"]; // right
Because you need the filename of the temporary copy stored by PHP, which exists on the server:
因为你需要PHP存储的临时副本的文件名,它存在于服务器上:
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII); // wrong
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
FTP_ASCII); // right
Refer to the PHP documentationfor more information about $_FILES.
有关 $_FILES 的更多信息,请参阅PHP 文档。
回答by Kyle
Are you sure that the folder you are uploading to has the correct permissions? Try chmoding it to 777 and see if that works.
您确定要上传到的文件夹具有正确的权限吗?尝试将其修改为 777,看看是否有效。
回答by Darvex
The file is stored on server with temporary name, so when you try uploading $_FILES['file']['name'], it fails, because file with such name does not exist. Instead you should call ftp_put()with $_FILES['file']['tmp_name']
该文件以临时名称存储在服务器上,因此当您尝试上传时$_FILES['file']['name'],它会失败,因为具有此类名称的文件不存在。相反,你应该打电话ftp_put()给$_FILES['file']['tmp_name']
It's explained a little better here

