How to make a page keep loading a file (.txt) even its not there yet?
I want to load a txt file which is not there yet. I mean I'll get a pending status from server. And the page will only get respond 200 sta开发者_JAVA技巧tus from server when the txt file is there. Or there is no way to do that?
do{
$file = @file_get_contents("file.txt");
sleep(1);
}while(empty($file));
If we are talking about remote server, you should use cURL, and "load" file every couple of seconds, if string is empty, means there is no file. This should be checked async.
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close ($ch);
return curl_exec($ch);
}
do{
$file = curl("http://example.com/file.txt");
sleep(5);
}while(empty($file));
$.post("your/url", {}, function(file) {
// since this is a success callback, once you are inside this block,
// you got your file.
// If you return anything other than 200 OK, this callback wouldn't get called
});
精彩评论