6

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;
    }
}
8
  • 2
    $status never changes its value.
    – u_mulder
    Commented Mar 26, 2017 at 10:35
  • 1
    "While status is not true, if status is true…" – there's a lot wrong with this attempted logic.
    – deceze
    Commented Mar 26, 2017 at 10:37
  • i had tried changing it to be direct, see my edit but it still doesn't work, the second echo never happens also.
    – zeddex
    Commented Mar 26, 2017 at 10:38
  • 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.
    – deceze
    Commented Mar 26, 2017 at 10:39
  • 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.
    – zeddex
    Commented Mar 26, 2017 at 10:43

4 Answers 4

7

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")) {
        printf('The file was found: %s', date("d-m-Y h:i:s"));
        break;
    }
    sleep(1);   //  or whatever …
} while(!file_exists("test.txt"));
2
  • Thanks, i don't usually use while loops so i will make a note of this.
    – zeddex
    Commented Mar 26, 2017 at 10:53
  • Do some sleeps, for gods sake :)
    – dkellner
    Commented Jan 2, 2023 at 14:26
5

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);
1
  • if you were to add in a sleep(1); that would be directly after the if statement right?
    – zeddex
    Commented Mar 26, 2017 at 11:12
3

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.