繁体   English   中英

[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);

有谁知道可能是什么问题吗?

非常感谢!

我看到您热衷于使用面向对象的方法,因此首先建议您使用DirectoryIterator类,该类将使您能够很好地检测刚刚加载的“文件”是否不是点(“。”或“ ..”)或目录。

$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;
        }

    }
}

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM