2012-03-25 5 views
0

このようなファイルやディレクトリはありません。[function.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 

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 

1ファイル "1g.jpg"は問題なく動作します。私はそれらの名前を変更しました。コンテンツではなく、ファイル名です。あるいは、それは最初のものです。 私はファイルのパーミッションもチェックしましたが、名前の変更はトリッキーなので説明はしません。

Here's(だけでなく、別のディレクトリに正常に動作します)完全なコード :

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

誰もが、問題が何であるか任意の手掛かりを持っていますか?

ありがとうございました!

+1

各繰り返しで$ファイルの内容を確認しましたか? – Daxcode

答えて

0

私はあなたがオブジェクト指向のアプローチを利用したいと思っていることを知っているので、最初に読み込んだ "ファイル"がドットではないかどうかをうまく検出できる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; 
     } 

    } 
} 

これが役立ちます。

関連する問題