Reputation: 199
I have a simple code in PHP that after move a uploaded file, need to print some information, but the problem is, I have a Sleep() inside the IF statement, and all the text before and after the sleep just show when the sleep count end.
Code below:
if(move_uploaded_file($_FILES['uploadvideo']['tmp_name'],$target_path))
{
echo "<pre>";
echo "Your video ".$cname." has been successfully uploaded<br>";
echo sha1_file($target_path)."<br>";
echo md5_file($target_path)."<br>";
ob_end_flush();
flush();
sleep(2);
echo "Please check if your file is good";
echo "</pre>";
}
If I put the flush, and sleep outside the IF everything works good.
The question is the Sleep() statement can be inserted inside a IF?
Thanks!
Upvotes: 1
Views: 540
Reputation: 72396
sleep()
works fine. But it cannot help you achieve your goal.
flush()
cannot force sending the content you generate before sleep()
back to the client (the browser). All it does is to push it down the line. But it cannot control what happens with the text when it exits the interpreter. The web server can cache the content and send it together with the rest of the content when the script ends. And, sometimes, the browser itself doesn't render a piece of text until it receives it completely.
It is explained in the documentation of flush()
.
You can use JavaScript and AJAX to achieve the effect you want (and, in fact, to implement it correctly).
Upvotes: 1
Reputation: 50
You need to specify charset
header('Content-type: text/html; charset=utf-8');
and it still might not work. Try flushing after every echo statement, and even then not every one might get flushed. Flushing output buffer only sends the data to output, it is not guaranteed to send it to the browser.
Upvotes: 0