How JS from DOM can learn about the appearance of .php files on the server?

Through setInterval to do AJAX requests?

  • This works in my case, but a bunch of 404 ошибок accumulating when there is no file.

Maybe there is a more correct way to notify JS about the appearance of files?

  • 1. websockets 2. And why not make a request through setInterval not to the file, but to something that will return the file or None if there is no file? - Axenow
  • @Axenow to something, this to what? - Vipz
  • At first did not understand your comment. Create another route that will check for the presence of a file for a specific user. Just if you give an example of the behavior of the program, I can help a little better. - Axenow

2 answers 2

If you know the names of the php files that should appear on the server, then through ajax a request is made to the following php file:

 <?php $filename = "test.php"; // имя файла, появление которого отслеживается //$filename = $_GET["filename"]; // это строка, если вдруг передавать имя файла из ajax-запроса // методом GET с параметром filename, например, // http://www.site.ru/check_file.php?filename=test.php // Тогда первая строка $filename = "test.php" не нужна if (file_exists($filename)) { echo "exists"; } else { echo "not exists"; } ?> 

Files will be scanned in the same directory as the called script. In response, an ajax request will come either "exists" (if the file exists) or "not exists" (if the file does NOT exist). Or to form a json-answer instead of the plain text "exists" and "not exists".

  • I didn’t get it yet, but for everyone I’ll ask, $filename = $_GET["filename"]; - This is if the name only knows JS and how to write it into a PHP variable - right? - Vipz
  • Yes exactly. Those. $ _GET ["filename"] reads the value of the variable "filename" after the equal sign in the link site.ru/check_file.php?filename=test.php , which (link) can already be generated using JS. If there is a POST-Ajax request, then instead of $ _GET use $ _POST - Alien Guest

It depends on what load is expected and on what end goal. The simplest is an AJAX request. Make a request for a route that will check file_exists() and return the result in JSON. You can web sockets, yes, but you have to sit and study.