无法包含文件php

时间:2014-06-19 07:39:12

标签: php

我可以使用get_included_files获取包含已包含或必需文件名称的数组。

但是有没有办法获取未能包含的文件列表?

例如:某些php文件包含

include_once("abc.php");

运行它会生成:failed to open stream: No such file or directory

我想返回此类文件的名称,在本例中为"abc.php"

1 个答案:

答案 0 :(得分:3)

使用自己的错误处理程序set_error_handler并在函数/方法中检查所需的错误消息,并使用它做一些事情。

<?php

// We want to see all errors of any type
error_reporting(E_ALL);

// Set our own error handler to catch E_WARNING errors
set_error_handler(function($errno, $errstr, $errfile, $errline, $errcontext) {
    // Strip out these warnings because an invalid include throws two warnings
    if(strpos($errstr, 'failed to open stream: No such file or directory') !== false)
        return true;

    // Extract the file name out of the warning message string
    if(preg_match('/Failed opening \'(.+?)\'/', $errstr, $matches) !== 1)
        return false;

    $rscFile = $matches[1];

    echo 'file include not possible: ' . $rscFile . '<br>';
    return true;
}, E_WARNING);

// Generate some errors
include_once('no-file-1.php');
include_once ('no-file-2.php');
include_once 'no-file-3.php';
include('no-file-4.php');
include 'no-file-5.php';
include ('no-file-6.php');

$arr = array('apple');

array_pop(array_shift($arr), 5);

echo $noVar;

require 'afile.php';