Search code examples
phpwhile-loopwait

PHP - Wait for file to exist


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;
    }
}

Solution

  • 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);