Home > Software engineering >  PHP file_exists not working (multiple execution in same browser)
PHP file_exists not working (multiple execution in same browser)

Time:05-23

I'm trying to understand why this script doesn't work, when I execute script simultaneously in same browser or browser tab, second script do not see the created file "/tmp/monkey.tmp" (php7.4-fpm nginx, default config, opcache enabled)

As soon as I used two different browsers, it works like expected, if I execute same script/URL simultaneously and one script with random data for example URL?_=monkey, it works like expected, problem is same URL in same browser, I dont understand why

$tmpfile = '/tmp/monkey.tmp';
                
clearstatcache();                                                                             
if(file_exists($tmpfile))
{
    die('file exist');
}
else
{
    file_put_contents($tmpfile, 'blabla');     
}

sleep(20);

exit;

CodePudding user response:

Php can be tricky, when debugging with browsers because they most likely cache the php page, tho that is not the wanted bahaviour.

My guess is the browser or webserver cached the site and that is why you don't see the change. But on the other Browser that didn't cache does.

That also explains why you see the change in the same browser when cahnign some part of the url, because the browser treats it as another page and can not use it's cache and goes to the server.

To debug u can try the following:

  • Run script on one Browser and see whether the file got created by hand
  • If thats the case and after refreshing you still don't get file exists -> Browser Cacheing

Try to install a plugin to clear the cache.

To stop this you can try and disable caching for this site in your browsers devtools.

Or set matadata cache time in the html to never cache the site.

Or run the request via Js that then prints the result to the page.

If after the cache clear you still don't see the page exists. It maybe the webserver caching the response.

Tho thats unlikely...

  • Related