How to determine whether a file is still being transferred via ftp

后端 未结 5 1561
情书的邮戳
情书的邮戳 2020-12-06 15:59

I have a directory with files that need processing in a batch with PHP. The files are copied on the server via FTP. Some of the files are very big and take a long time to co

5条回答
  •  猫巷女王i
    2020-12-06 16:44

    Best way to check would be to try and get an exclusive lock on the file using flock. The sftp/ftp process will be using the fopen libraries.

    // try and get exclusive lock on file
    $fp = fopen($pathname, "r+");
    
    if (flock($fp, LOCK_EX)) {  // acquire an exclusive lock
        flock($fp, LOCK_UN);    // release the lock
    fclose($fp);
    }
    else {
        error_log("Failed to get exclusive lock on $pathname. File may be still uploading.");
    }
    

提交回复
热议问题