Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 314
zeddex Avatar asked Mar 26 '17 10:03

zeddex


People also ask

How to check if a file exists in PHP?

There are three different functions that you can use to check if a file exists in PHP. The first function is file_exists (). This function accepts a single parameter that is the path where your file is located. Keep in mind that it will return true for both existing files and directories.

What does @file_exists () do in PHP?

file_exists () does NOT search the php include_path for your file, so don't use it before trying to include or require. Yes, include does return false when the file can't be found, but it does also generate a warning. That's why you need the @. Don't try to get around the warning issue by using file_exists ().

Does file_exists () search the PHP include_path?

Don't try to get around the warning issue by using file_exists (). That will leave you scratching your head until you figure out or stumble across the fact that file_exists () DOESN'T SEARCH THE PHP INCLUDE_PATH.

Why does inotifywait wait forever?

Then inotifywait would wait forever for a file that is already here. You'd need to make sure the file is not already there after the watch has been installed. some of the solutions leave inotifywait running (at least until another file is created) after the file has been found.


1 Answers

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);
like image 67
hassan Avatar answered Oct 05 '22 11:10

hassan