php how to test if file has been uploaded completely

后端 未结 11 1571
耶瑟儿~
耶瑟儿~ 2021-02-01 15:24

Is there any way to check if file has been uploaded completely on the server? My scenario: User uploads file over ftp and my other PHP task is running in cronjob. Now I would li

11条回答
  •  旧巷少年郎
    2021-02-01 16:07

    By checking upload error message you can confirm whethere file complete uploaded or partial uploaded

    If upload Error Code is 3 then file uploaded partially

    let say your file upload field name myfile

    $error=$_FILES['myfile']['error'];
    if($error>0){
    //Upload Error IS Present check what type of error
    switch($error){
        case 1:
            // UPLOAD_ERR_INI_SIZE
            // Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
            break;
        case 2:
            // UPLOAD_ERR_FORM_SIZE
            // Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
            break;
        case 3:
            // UPLOAD_ERR_PARTIAL
            // Value: 3; The uploaded file was only partially uploaded.
            break;
        case 4:
            // UPLOAD_ERR_NO_FILE
            // Value: 4; No file was uploaded.
            break;
        case 6:
            // UPLOAD_ERR_NO_TMP_DIR
            // Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
            break;
        case 7:
            // UPLOAD_ERR_CANT_WRITE
            // Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
            break;
        case 8:
            // UPLOAD_ERR_EXTENSION
            // Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.
            break;
    }
    
    }else{
        //File IS Uploaded you can move or validate other things
    }
    

    In case 3 you can check partial upload

    If you want to Track file Upload Via FTP Programme if you are running VSFTPD then you can track

    file=/var/log/vsftpd.log
    initial_files=`grep -c 'OK UPLOAD' $file`;
    while [ TRUE ]
    do
    current_files=`grep -c 'OK UPLOAD' $file`;
    if [ $current_files == $initial_files ]; then
         echo "old File Not Uploaded";
    else
         echo "new File Uploaded Process New File";
    new_file=`grep 'OK UPLOAD' $file | tail -1`;
    echo $new_file;
    initial_files=$current_files;
    fi
    sleep 1
    done
    

    any problem in understanding please reply

提交回复
热议问题