php how to test if file has been uploaded completely

后端 未结 11 1532
耶瑟儿~
耶瑟儿~ 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:16

    Check for the file after uploading by next code

    if (file_exists('http://www.domain.com/images/'.$filename)) :
    
        // Write some response as you like as a success message
    
    endif;
    

    Notice: you have to change images path by yours ,

    good luck :)

    0 讨论(0)
  • 2021-02-01 16:17

    if you are running php on linux then lsof can help you

    $output = array();
    exec("lsof | grep file/path/and/name.ext",$output);
    if (count($output)) {
      echo 'file in use';
    } else {
      echo 'file is ready';
    }
    

    EDIT: in case of permission issue. by using sudo or suid methods php script can get required permissions to execute lsof command. to set suid you have to issue following command as root.

    su root
    chmod u+s /usr/sbin/lsof
    
    0 讨论(0)
  • 2021-02-01 16:18

    keep sql record of date modified and check if you have newely modified file.

    <?php 
    $filename = 'somefile.txt';
    if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
    }
    ?>
    
    0 讨论(0)
  • 2021-02-01 16:23
    <?php
        if (move_uploaded_file($_FILES["uploader_name"]["tmp_name"], $path_name)):
            //print true condition
        else:
            //print false condition
        endif;
    ?>
    
    0 讨论(0)
  • 2021-02-01 16:30

    If you have control over the application doing the upload, you can require that it upload the file to name.tmp, and when it's done uploading rename it to name.final. Your PHP script could look only for *.final names.

    0 讨论(0)
提交回复
热议问题