【问题标题】:File uploaded with ftp_nb_put to FileZilla FTP server in PHP is corrupted在 PHP 中使用 ftp_nb_put 上传到 FileZilla FTP 服务器的文件已损坏
【发布时间】:2024-01-17 19:53:01
【问题描述】:

我正在尝试使用 html 表单上传文件。上传方法是通过 FTP 到 FileZilla 服务器。我已经成功上传了文件,但似乎只有文件扩展名和文件名是上传的。在 Windows 资源管理器中查看传输的文件时,该文件始终已损坏并且不显示其文件大小。这是我的代码。请帮忙。

include 'Connections/ftpgangconnect.php';
ini_set("upload_max_filesize", "250M");
ini_set("post_max_size", "250M");

if (isset($_POST['upload'])) {
    $name = $_FILES['myfile'] ['name'];
    $size = $_FILES['myfile'] ['size'];
    $type = $_FILES['myfile'] ['type'];
    $temp = $_FILES['myfile'] ['tmp_name'];
    $error = $_FILES['myfile'] ['error'];
    $content = $_FILES['myfile']['content'];
    $temp = $_FILES['myfile'] ['tmp_name'];
    $name = $_FILES['myfile']['name'];
    $dest_file ="/".$name;

    // upload the file
    $upload = ftp_nb_put($ftp, $dest_file, $temp, FTP_BINARY);

    // check upload status
    if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $name";
        ftp_close($ftp);
    }
}

【问题讨论】:

    标签: php ftp


    【解决方案1】:

    查看ftp_nb_put function 的示例。您应该循环调用ftp_nb_continue,直到上传完成:

    // upload the file
    $upload = ftp_nb_put($ftp, $dest_file, $temp, FTP_BINARY);
    
    while ($upload == FTP_MOREDATA)
    {
       // Continue uploading...
       $upload = ftp_nb_continue($ftp);
    }
    

    尽管除非您需要在循环中执行任何其他操作,否则使用ftp_nb_put 毫无意义。只需使用简单的ftp_put 代替:

    $upload = ftp_put($ftp, $dest_file, $temp, FTP_BINARY);
    

    【讨论】: