消除有关simplexml_load_file PHP的警告

时间:2013-08-15 10:59:35

标签: php xml warnings

我首先扫描目录树并使用simplexml_load_file()从xml文件中检索数据。该功能正常,但我收到警告:

Warning: simplexml_load_file(startfolder/cyrilic) [function.simplexml-load-file]: failed to open stream: Permission denied in C:\xampp\htdocs\begin\xxx.php on line 36

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "startfolder/cyrilic" in C:\xampp\htdocs\begin\xxx.php on line 36  

当有更改子文件夹时,会出现此警告。 我在php.ini中设置:

allow_url_fopen = On
allow_url_include = On

代码是:

$dir    = 'startfolder';
        $items = glob($dir . '/*');
        $table = 'book';
         for ($i = 0; $i < count($items); $i++) {
             if (is_dir($items[$i])) {
                 $add = glob($items[$i] . '/*');
                 $items = array_merge($items, $add);
             }
         }
        echo "<ul id='booklist'>"."</n>";
        foreach ($items as $key=>$file){
            $url = $file;
            $xml = simplexml_load_file($url);  
            $book_count = count($xml->book); 

1 个答案:

答案 0 :(得分:1)

检查您是否可以先读取该文件:

foreach ($items as $key=>$file){
    if(is_readable($file) == false) {
        echo "$file unreadable";
        continue;
    }