ftp_put返回TRUE但没有上传文件

时间:2017-12-04 08:34:31

标签: php file-upload ftp

尝试使用HTML表单和PHP将文件上传到FTP服务器。我的HTML表单是:

<!DOCTYPE html>
<html>
<body>

<form action="uploader.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="files" id="files">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

uploader.php:

<?php
$conn_id = ftp_connect ( 'ftp.mydomain.com' );
$ftp_user_name="<username>";
$ftp_user_pass="<password>";

// 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!" ;
    exit;

} else {
    echo "Connected to for user $ftp_user_name" ;
}

if (empty($_FILES['files']['tmp_name'])) {
    echo "No file!!!!!!!!!";
    die();
}
// upload the file
$upload = ftp_put( $conn_id, "uploads/" , $_FILES['files']['tmp_name'] , FTP_BINARY );

// check upload status
if(!$upload){
    echo "FTP upload has failed!" ;
} else {
    echo "Successfully Uploaded." ;
}
?>

我得到的输出是Successfully Uploaded.,但实际上找不到在服务器上传的文件。

uploads/目录位于同一个uploader.php目录中。尝试将其更改为uploads/somename.zip会显示uploads/somename.zip is not a valid directoty

1 个答案:

答案 0 :(得分:1)

ftp_put的第二个参数是远程文件的路径,而不是本地目录的路径。

所以应该是这样的:

ftp_put($conn_id, "/remote/path/somename.zip", $_FILES['files']['tmp_name'], FTP_BINARY);

此外,您应该使用被动模式。请参阅PHP ftp_put fails