Every help would be great on this very strange error!
I´m looping through the files of a directory to detect their MIME-Types. All of them, EXCEPT one, throw the following errors:
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
One file, "1g.jpg" works without problems! I´ve renamed them, it´s not the content, it´s the filename, OR, I suppose, the fact, that it´s the first one. I´ve also checked file permissions, but since renaming does the trick that´s not an explanation either of course.
Here´s the complete code (which works fine in another directory as well):
$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);
Does anyone have any clue what the problem might be?
Thanks very much!
I see you're keen on using object oriented approach, so first of all I'd suggest using a DirectoryIterator class, which will allow you to nicely detect whether the "file" you just loaded isn't a dot ("." or "..") or a directory.
$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;
}
}
}
Hope this helps.