[function.file-get-contents]发生了一些奇怪的事情:无法打开流:中没有这样的文件或目录


quite strange things going on with [function.file-get-contents]: failed to open stream: No such file or directory in

对于这个非常奇怪的错误,任何帮助都会很棒!

我正在循环浏览目录中的文件,以检测它们的MIME类型。除一个错误外,所有这些错误都抛出以下错误:

Warning: file_get_contents(3g.jpg) [function.file-get-contents]: failed to open stream: No such file or directory in /Library/WebServer/Documents/V10/getfiles.php on line 46
Warning: file_get_contents(4g.jpg) [function.file-get-contents]: failed to open stream: No such file or directory in /Library/WebServer/Documents/V10/getfiles.php on line 46

一个文件,"1g.jpg"工作没有问题!我已经重命名了它们,这不是内容,而是文件名,或者,我想,事实上,这是第一个。我也检查过文件权限,但由于重命名确实有效,这当然也不是一个解释。

这是完整的代码(在另一个目录中也可以正常工作):

$handle=opendir ($dir);
$Previews_php=array();
while ($file = readdir ($handle)) {
    $file_info = new finfo(FILEINFO_MIME);  // object oriented approach!
    $mime_type = $file_info->buffer(file_get_contents($file));  // e.g. gives "image/jpeg"
    if (preg_match("/image/",$mime_type,$out)) {
        $Bilder_php[]= $file;
    }
}    
closedir($handle);

有人知道问题出在哪里吗?

非常感谢!

我知道你热衷于使用面向对象的方法,所以首先我建议使用DirectoryTerator类,它可以很好地检测你刚刚加载的"文件"不是点("."或"..")还是目录。

$images   = array();
$dirName  = dirname(__FILE__) . '/images';   // substitute with your directory
$handle   = new DirectoryIterator($dirName);
$fileInfo = new finfo(FILEINFO_MIME);        // move "new finfo" outside of the loop;
                                             // you only need to instantiate it once
foreach ($handle as $fi) {
    // ignore the '.', '..' and other directories
    if ($fi->isFile()) {                    
        // remember to add $dirName here, as filename will only contain 
        // the name of the file, not the actual path
        $path     = $dirName . '/' . $fi->getFilename();

        $mimeType = $fileInfo->buffer(file_get_contents($path)); 
        if (strpos($mimeType, 'image') === 0) { // you don't need a regex here, 
                                                // strpos should be enough.
            $images[] = $path;
        }
    }
}

希望这能有所帮助。