This is the code

if(file_exists('1.mp4')){
echo "File Exists";
//Performing some operations on file
}
else{
    echo "File Does Not Exists";
}

Now, problem is "if" condition get's satisfied when file is being uploading.
File upload is in process then I am performing some operation's on that file.
But as upload/copy/creation is in process that file is incomplete and here is the problem.

How to wait after file_exists till file successfully get's uploaded/copied/created. Then perform operation's on it ?

有帮助吗?

解决方案

First of all, I wouldn't recommend this unless it's the last possible option. Knowing when a file is ready is better than guessing whether a copy operation has finished.

Even if a file no longer grows doesn't mean the contents are actually valid; perhaps the copy operation was aborted and a partial file remains; without a checksum you would have no idea whether the file can actually be used.

function waitFor($file, $delay = 1)
{
    if (file_exists($file)) {
        $current_size = filesize($file);
        while (true) {
            sleep($delay);
            clearstatcache(false, $file); // requires >= 5.3
            $new_size = filesize($file);
            if ($new_size == $current_size) {
                break;
            }
            $current_size = $new_size;
        }

        return $current_size;
    } else {
        return false;
    }
}

waitFor('/path/to/file', 2); 
// consider file is ready if size not changed for two seconds
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top