使用PHP将所选文件上传到FTP

时间:2017-05-10 11:13:05

标签: php file ftp upload

我希望使用PHP将选定的本地文件上传到FTP。

使用$_POST方法选择本地文件:

<form  method="POST" ENCTYPE="multipart/form-data">
   <input type="file" name="filename"/><br/>
   <input type="submit" value="Upload"/>
</form>

这次上传尝试:

$source_file=$_FILES['filename']['tmp_name'];
$remote_file='/www/img/file.txt';



if (ftp_put($conn_id, $remote_file, $source_file, FTP_BINARY)) 
{
echo "successfully uploaded $source_file\n";
} 
else 
{
echo "There was a problem while uploading $source_file\n";
}

结果:

  

警告:ftp_put():文件名不能为空

$_FILES['filename']['tmp_name']为空。

我知道$source_file必须是一条道路。 所以,我有疑问:所选文件的正确路径是什么?

1 个答案:

答案 0 :(得分:0)

您应该按照以下步骤操作

(1)首先将上传的文件移至本地服务器

$uploads_dir = '/uploads';

$remote_file='/www/img/file.txt';

$source_file=$_FILES['filename']['tmp_name'];

$name = basename($_FILES["filename"]['tmp_name']);

$file_path = $uploads_dir."/".$name;

move_uploaded_file($source_file, $file_path);

(2)将本地服务器文件路径提供给ftp upload

if (ftp_put($conn_id, $remote_file, $file_path, FTP_BINARY)) 
{
echo "successfully uploaded $source_file\n";
} 
else 
{
echo "There was a problem while uploading $source_file\n";
}