Jump to content

Why does it take so long to recognize file existence?


evansste

Recommended Posts

I'm working on a website that displays data based on when a file is created.  The page keeps checking to see whether or not a file exists.  As long as the file doesn't exist, the page keeps reloading and checking to see whether or not the file exists until the file finally does exist.  Once the file exists, the page goes to another page that displays the contents of the file that it was waiting for.

This design works, but I've noticed that it takes a long time for the page to recognize that the file exists.  Once the file exists, the page keeps checking and reloading, several times -- even though the file exists.  After many reloads, it finally "sees" the file and goes on to the next page.  Why does it take so long for my page to recognize the existence of the file?

Here's a sample of how the reloading page (the page that keeps checking for the existence of the file) works.

 

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
session_set_cookie_params(0);
session_start();
?>

<html>

<head>
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
</head>

<body>

<p>Processing...</p>


<script>
myFunction();

function myFunction() {
    setInterval(function(){

location.reload();

<?php
clearstatcache();
if(file_exists($_SESSION['filename']))
{
  $existance = true;
}
else
{  
  $existance = false;
}
?>

if("<?php echo $existance ?>")
{
  window.open("done.php","_self");
}
else
{
}

}, 5000);
}
</script>

</body>
</html>


As you can see, I use the "clearstatcache();" statement in order to make sure that the server's information is current before checking the existence of the file.  I have also used meta tags, in the HTML, and php headers in order to make sure that nothing is being cached.  This is all in an attempt to make sure that it's not looking at old data when it checks to see if the file exists.  Despite all of this, the browser keeps reloading, over and over again, usually about ten times, even after the file exists.  Any ideas why this is happening?



 

Edited by Maq
Link to comment
Share on other sites

are you sure that $_SESSION['filename'] itself is set? there could be an issue with the session data file being locked by the back-ground process that's setting $_SESSION['filename'] and the foreground file cannot read the session data until the back-ground process has released the lock on the file by either ending or using session_write_close().

 

what is the back-ground processing code and why is it taking so long to create the file in the first place that you couldn't just dynamically output the file when needed?

 

clearstatcache(); would only be needed if the php code is looping. since the page itself is being re-requested, the php code is starting from scratch each time and it would get the current status of the file.

 

you have stated you know that the file has actually been created at some point, but that the posted code is not detecting its existence? how exactly are you determining that the file has been created? what sort of tool are you using to know this information?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.