简体   繁体   中英

Get PHP to wait until a file is done transferring before moving it

I have a PHP script that moves files out of a specific folder on the server(an IBM AS400). The problem I am running into is that sometimes the script runs while the file is still in the process of being moved in to the folder.

Poor logic on my part assumed that if a file was "in use" that PHP wouldn't attempt to move it but it does which results in a corrupted file.

I thought I could do this:

$oldModifyTime = filemtime('thefile.pdf');
sleep(2);
if($oldModifyTime === filemtime('thefile.pdf'){
  rename('thefile.pdf','/folder2/thefile.pdf');
}

But the filemtime functions come up with the same value even while the file is being written. I have also tried fileatime with the same results.

If I do Right Click->Properties in Windows the Modified Date and Access Date are constantly changing as the file is being written.

Any ideas how to determine if a file is finished transferring before doing anything to it?

将其作为临时名称或其他文件夹传输,然后在传输完成后将其重命名/复制到正确的文件夹。

From the PHP manual entry for filemtime() :

Note: The results of this function are cached. See clearstatcache() for more details.


I would also suggest that 2 seconds is a bit short to detect whether the file transfer is complete due to network congestion, buffering, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM