首页 > 解决方案 > 使用 PHP 持续检查文件是否存在

问题描述

我正在做一个项目,该项目要求我每 5 秒检查一次 PHP 文件是否存在,如果该文件存在,则重定向到该文件。

如果它不存在,则脚本应改为每 5 秒检查一次。

我该怎么做呢?我知道 file_exists(),但我将如何让它连续检查,而不仅仅是一次?

标签: phpfileexists

解决方案


你可以尝试使用这个

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

推荐阅读