Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 搜索并打开文件_Php_File_Search_While Loop_Timeout - Fatal编程技术网

Php 搜索并打开文件

Php 搜索并打开文件,php,file,search,while-loop,timeout,Php,File,Search,While Loop,Timeout,我有一个脚本,我已经做了到目前为止,寻找一个指定的文件在当前目录,如果它不在那里,它将上升到一个目录,并再次搜索 如果文件存在,脚本工作正常,但是如果它不存在,脚本将继续运行,直到脚本被取消超过30秒,即使使用计数器限制执行 $path = 'log.log'; $file_exists = 0; $search_count = 0; $search_limit = 3; while($file_exists == 0) { while($search_count < $se

我有一个脚本,我已经做了到目前为止,寻找一个指定的文件在当前目录,如果它不在那里,它将上升到一个目录,并再次搜索

如果文件存在,脚本工作正常,但是如果它不存在,脚本将继续运行,直到脚本被取消超过30秒,即使使用计数器限制执行

$path = 'log.log';

$file_exists = 0;

$search_count = 0;
$search_limit = 3;

while($file_exists == 0) {
    while($search_count < $search_limit) {
        if(file_exists($path)) {
            $file_exists = 1;
            $search_count = $search_limit + 1;

            $resource = fopen($path, "r");  
            while (!feof($resource)) {
               echo fgetss($resource);
            }
            fclose($resource);
        } else {
            $path = '../'.$path;
            $search_count++;
        }
    }
}
$path='log.log';
$file_exists=0;
$search\u count=0;
$search_limit=3;
而($file_exists==0){
而($search\u count<$search\u limit){
如果(文件_存在($path)){
$file_exists=1;
$search\u count=$search\u limit+1;
$resource=fopen($path,“r”);
而(!feof($resource)){
echo fgetss(资源);
}
fclose(资源);
}否则{
$path='../'.$path;
$search_count++;
}
}
}
将是无限的,因为只有在找到文件时才将
$file\u exists
设置为
1

假设文件不在那里,那么内部循环将只运行三次,而外部循环将无限运行(尽管没有任何可执行语句)

编辑:

您可以将这些条件合并为

while($file_exists == 0 && $search_count < $search_limit) {

//your entire code

}
while($file\u exists==0&&$search\u count<$search\u limit){
//你的全部代码
}

我想你正在寻找这样的东西:

$path = 'log.log';
$file_exists = false;
$search_count = 0;
$search_limit = 3;

while (!$file_exists and $search_count < $search_limit) {
    if(file_exists($path)) {
        $file_exists = true;
        $resource = fopen($path, "r");
        while (!feof($resource)) {
           echo fgetss($resource);
        }
        fclose($resource);
    } else {
        $path = '../'.$path;
        $search_count++;
    }
}
...
if(file_exists($path)) {
    $file_exists = true;
    $contents = file_get_contents($path);
    echo $contents;
}
...

查找有关该方法的更多信息

如果文件不存在,则外部while是无限的,因为
$file\u exists=1从未被调用。哎呀,我的错。但是,即使如此,如果我交换文件_exists,并在该文件周围循环计数器,那么如果该文件不存在,您的内部循环将是无限的,这是一个如此简单的修复!非常感谢!不客气;)注意:这段代码开始在当前文件夹中查找日志文件。如果要开始寻找另一个路径,如
文件夹/log.log
,则必须修改代码。因为如果在“文件夹”中找不到该文件,则行
$path='../'.$path
将给出:
。/folder/log.log
。但是您需要的是
文件夹/./log.log
。要实现这一点,您可以在开始时将变量
$path
分离到:
$dir='folder/'
$file='log.log'
中,并修改相关行:
文件存在($dir.$file)
文件获取内容($dir.$file)
$dir=$dir.../'
但如果找不到文件
$path='../'.$path'
。$path'
将被执行,因此它将
$path='log.log'
转换为
$path='../log'
,并将其指定为新的
$path
。如果找不到,那么再找一次,它只是在
$path
的开头追加另一个
。/
,因此,每次尝试后,它基本上都会查找一个目录,直到找到为止。如果您想开始在当前目录中搜索并在目录树中按自己的方式搜索,这是没有问题的。但是,例如,如果您开始使用路径
folder/log.log
进行搜索,您最终会得到
。/../../../folder/log.log
。但这样你只在“文件夹”目录中搜索,而不在它的“兄弟”(在同一个父目录中)中搜索。要在这些“同级”目录中也进行搜索,您必须在文件名前面插入“../”,如:
文件夹/./log.log
,因为这与:
log.log
相同,
文件夹/../../../log.log
与:
./log.log
相同,依此类推。啊,我明白了。这对未来的项目非常有用,但对于这个特定场景,我要查找的文件永远不会在同级文件夹中。如果它不在当前的一个,它必须在它上面的一个,或者在它上面的一个,但它永远不会回到树下
...
if(file_exists($path)) {
    $file_exists = true;
    $contents = file_get_contents($path);
    echo $contents;
}
...