PHP - Wait for file to exist

php file exists
php check if image exists at url
php sleep
php file_exists url
php check image exists
php read file
php wait for include to finish
php wait before proceeding

I wanted to exec a exe which generates txt files and also in another script then check that the txt files have been created.

In xampp i am simply dragging in a test.txt file to the following php scripts dir but it doesn't seem to work correctly, also if i add in text.txt to the dir and start the script rather than starting before it is added then the second echo never seems to happen.

How can i make PHP Wait for the text file to exist and then continue?

set_time_limit(0);

echo "Script began: " . date("d-m-Y h:i:s") . "<br>";

$status = file_exists("test.txt");
while($status != true) {
    if ($status == true) {
        echo "The file was found: " . date("d-m-Y h:i:s") . "<br>";
        break;
    }
}

This also does not work:

set_time_limit(0);

echo "Script began: " . date("d-m-Y h:i:s") . "<br>";

while(!file_exists("test.txt")) {
    if (file_exists("test.txt")) {
        echo "The file was found: " . date("d-m-Y h:i:s") . "<br>";
        break;
    }
}

this should works fine

set_time_limit(0);

echo "Script began: " . date("d-m-Y h:i:s") . "<br>";

do {
    if (file_exists("test.txt")) {
        echo "The file was found: " . date("d-m-Y h:i:s") . "<br>";
        break;
    }
} while(true);

Returns TRUE if the file or directory specified by filename exists; FALSE otherwise. Note: This function will return FALSE for symlinks pointing to non-​existing files. In one scenario, only seven of the files existed. In the second, all files existed. is_file() needed 3.0 for scenario one and 3.3 seconds for scenario two. file_exists() needed 2.8 and 2.9 seconds, respectively. The absolute numbers are off course system-dependant, but it clearly indicates that file_exists() is faster. up.

I trust that you have other safeguards in place to make sure that you’re not in an infinite loop.

while(!file_exists('test.txt'));
echo "The file was found: " . date("d-m-Y h:i:s") . "<br>";

would be simpler.

Anyway, your problem is with your pretest. Since it fails to begin with, it never repeats. What you need is a post test:

do {
    if (file_exists("test.txt")) {
        echo "The file was found: " . date("d-m-Y h:i:s") . "<br>";
        break;
    }
} while(!file_exists("test.txt"));

$time = 4; //how long in seconds do you allow your program to run/search. $found = false; for($i=0; $i<$time; $i++){ if(file_exists('you/file/path/file.txt')){ //Found it. //do whatever. $found = true; break; the file can be exist and delted after by the rest of the code so what i need is to wait for the file to not exist ,if it is exist continue with the loop until it is not exist ,if it is not exist continue the rest of the code. thanx Markus

I suppose you should use this approach:

set_time_limit(0);

echo "Script began: " . date("d-m-Y h:i:s") . "<br>";

while (true) {
    // we will always check for file existence at least one time
    // so if `test.txt` already exists - you will see the message
    // if not - script will wait until file appears in a folder
    if (file_exists("test.txt")) {
        echo "The file was found: " . date("d-m-Y h:i:s") . "<br>";
        break;
    }
}

To solve the problem, I wrote a quick little powershell script to check if the file exists and then wait in a loop before starting the SSIS package. Well, file_exists does not say if a file exists, it says if a path exists. ⚡⚡⚡⚡⚡⚡⚡ So, to check if it is a file then you should use is_file together with file_exists to know if there is really a file behind the path, otherwise file_exists will return true for any existing path.

Brief experiments suggest that waiting for an asynchronous file system change (using PHP as a module in Apache) must yield control in the loop. Otherwise, the number of loops spent waiting (such as for a file to be deleted by unlink()) seems to be random over a very large range. Yielding inside such a loop can be done by "usleep(250000)", which will yield control for 1/4 of a second.

how do you wait until a file has been fully written by an external process in php? i'​m trying to use this function sleepUntilWritten($filename){  Definition and Usage. The file_exists() function checks whether a file or directory exists. Note: The result of this function is cached. Use clearstatcache() to clear the cache.

Or maybe I'm missing what you are trying to do. That loop will work file if you use it to check for the existence of the file that you are actually  Regarding the change in PHP 5.3.2 with locked files: Without having studied the PHP source code in detail, the situation appears to be as follows when the PHP function fclose() is called: Before 5.3.2 PHP would check if the file was locked, then release the lock, and then close the file. From 5.3.2 PHP just closes the file.

PHP file_exists() Function. ❮ PHP Filesystem Reference. Example. Check whether a file exists: <?php echo file_exists(  Like @zane-hooper, I've had a similar problem on NFS. On parallel / distributed filesystems the lag between you creating a file on one machine and the other machine "seeing" it can be very large, so I could wait up to a full minute after the creation of the file before the while loop exits (and there also is an aftereffect of it "seeing" an already deleted file).

The fopen() function, when called on a file, does not stop that same file from script, it will wait ("block") on the flock() call and wait for the first script to finish. Because this app sometimes doesn't unpack the gz file, and sometimes doesn't rename the txt file to the final *.log file. FileSystemWatcher2 catches txt file, then (in the most cases it is renamed to log in the next 1000ms) I need to wait some time to check if txt file exists (if not, it seems to be renamed to the final *.log file).

Comments
  • $status never changes its value.
  • "While status is not true, if status is true…" – there's a lot wrong with this attempted logic.
  • i had tried changing it to be direct, see my edit but it still doesn't work, the second echo never happens also.
  • Your second attempt is better, but "while not exists, if exists" still makes no sense. That second piece at least leaves the loop when the file exists, contrary to the first version.
  • if it's while(file_exists("test.txt") then the echo runs as expected but it doesn't wait like needed. Usually i don't do this type of thing so im a bit confused as to what else might work.
  • if you were to add in a sleep(1); that would be directly after the if statement right?
  • Thanks, i don't usually use while loops so i will make a note of this.