I always get not exists even if the file is there. - What's wrong ?

 function checkedFile() { var fileName = 'file.php'; var checked = <?php $filename = $_GET["fileName"]; if (file_exists($filename)) { echo "'exists'"; } else { echo "'not exists'"; }?>; console.log(checked); } setInterval(function(){checkedFile()},500); 
  • Because "Everything is mixed up in the Oblonsky house" (c) Something you mixed JS and php in some particularly perverse way. - Stepan Kasyanenko
  • @StepanKasyanenko and how else? - Vipz

1 answer 1

You need to do an honest request for api, and check the file on the server, not on the client

 function checkedFile() { var fileName = 'file.php?fileName=имя_файла'; fetch(fileName).then(result => result.text()) .then(console.log) } setInterval(function(){checkedFile()},500); 

And on the server side already verify that the file exists

 filename = $_GET["fileName"]; if (file_exists($filename)) { echo "'exists'"; } else { echo "'not exists'"; } 
  • It seems that this example will fall when trying to parse json'ki, which is not json'ka at all. For the rest, plus. - Oleg
  • Where $_GET["fileName"] data come from in $_GET["fileName"] , if you don’t transfer it to fetch ? - Stepan Kasyanenko
  • one
    @StepanKasyanenko corrected - ThisMan