Php [function.file get contents]发生了非常奇怪的事情:无法打开流:中没有这样的文件或目录
在这个非常奇怪的错误上,每一个帮助都将是巨大的 我在目录的文件中循环,以检测它们的MIME类型。除一个错误外,所有这些错误都会引发以下错误:Php [function.file get contents]发生了非常奇怪的事情:无法打开流:中没有这样的文件或目录,php,file-get-contents,Php,File Get Contents,在这个非常奇怪的错误上,每一个帮助都将是巨大的 我在目录的文件中循环,以检测它们的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 Warn
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”可以正常工作!我重新命名了它们,不是内容,而是文件名,或者,我想,是事实,它是第一个。
我还检查了文件权限,但因为重命名可以做到这一点,当然这也不是一个解释
这是完整的代码
(在另一个目录中也可以正常工作):
有人知道问题出在哪里吗
非常感谢 我知道您热衷于使用面向对象的方法,因此首先我建议使用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;
}
}
}
希望这有帮助。您是否检查了每个迭代中包含的$file?
$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;
}
}
}