File uploaded with ftp_nb_put to FileZilla FTP ser

2019-04-16 04:20发布

问题:

I'm trying to upload a file using an html form. The upload method is via FTP to a FileZilla server. I have successfully uploaded the file but it seem like only it's file extension and file name are the ones that get uploaded. The file is always corrupted and doesn't display it's file size when viewing the transferred file in windows explorer. This is my code. Please help.

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);
    }
}

回答1:

Check the examples for the ftp_nb_put function. You should be calling the ftp_nb_continue in a loop, until the upload is finished:

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

while ($upload == FTP_MOREDATA)
{
   // Continue uploading...
   $upload = ftp_nb_continue($ftp);
}

Though unless you need to do anything else in the loop, its pointless to use ftp_nb_put. Just use a simple ftp_put instead:

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


标签: php ftp