James
James

Reputation: 135

Check continuously if a file exists with PHP

I am working on a project that requires me to check for the existence of a file with PHP every 5 seconds, and if the file exists, redirect to it.

If it doesn't exist, the script should keep checking every 5 seconds instead.

How would I go about doing that? I know about file_exists(), but how would I go about making it check continuously, not just once?

Upvotes: 0

Views: 1616

Answers (1)

Chukwuemeka Ihedoro
Chukwuemeka Ihedoro

Reputation: 605

you can try using this

<?php
$x = 0;
$count = 5;

do {
    if(!file_exists($file)){
        $x++;
        echo 'file loading';
        sleep(5);//Delays the program execution for 5seconds before code continues. 
    }
    else {
        header('Location: '.$file);
        exit();
    }
}
while($x < $count); // this kind of regulates how long the loop should last to avoid maximum execution timeout error

if($x == $count){
    echo 'file does not exist';
}
?>

Upvotes: 3

Related Questions