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;
}
}
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.
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 ().
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With