2012-05-02 4 views
0

私はyiiに新しく、システムフォルダ外のディレクトリ内にファイルのリストを取得する必要があります。私はシステムフォルダの外にあるディレクトリ内のすべてのファイルのリストを取得

 $fileListOfDirectory = array(); 
     $pathTofileListDirectory = Yii::app()->request->baseUrl.'/path/to/files'; 
     foreach(new DirectoryIterator($pathTofileListDirectory) as $file) { 
      if($file->isFile() === TRUE && $file->getBasename() !== '.DS_Store' && substr(strrchr($file->getPathname(), '.'), 1) == 'txt') { 
       array_push($fileListOfDirectory, $file->getBasename()); 
      } 
     } 

でそれを試してみましたが、私はオールウェイズエラーに

のDirectoryIteratorを取得:: __構築物(/ myYiiInstallation /システム/パス/ファイル/へ) [DirectoryIteratorの.--構築]: に失敗しましたDIRを開けないように:そのようなファイルやディレクトリ

をラインから

foreach(new DirectoryIterator($pathTofileListDirectory) as $file) { 

ファイルのリストを取得する別の方法がありますか? thnx!

+0

ねえ、コードが動作します - パスにエラーがありました:「/../」を追加する必要がありました。申し訳ありません - 私の悪い! – headkit

+1

本当に質問を削除するか、自分で回答を追加して受け入れるべきです。 – Paystey

答えて

1

あなたがこれを使用することができます....私はあなたの代わりにあなたのファイルシステムにbaseUrlにを使用してのhttp://www.yiiframework.com/doc/api/1.1/CApplication#basePath-detailを試みるべきだと思い

$fileListOfDirectory = array(); 
$pathTofileListDirectory = Yii::app()->request->baseUrl.'/path/to/files' ; 

if(!is_dir($pathTofileListDirectory)) 
{ 
    die(" Invalid Directory"); 
} 

if(!is_readable($pathTofileListDirectory)) 
{ 
    die("You don't have permission to read Directory"); 
} 

foreach (new DirectoryIterator ($pathTofileListDirectory) as $file) { 
    if ($file->isFile() === TRUE && $file->getBasename() !== '.DS_Store') { 

     if ($file->getExtension() == "txt") { 
      array_push ($fileListOfDirectory, $file->getBasename()); 
     } 
    } 
} 
+0

もちろん、それは私に 'txt'文字列を与えます。あなた自身で証明してください。しかし、ちょっと - これは私に私がここで解決する必要があるエラーを投げることはありません... ;-) – headkit

+0

ディレクトリを検証するために自分のコードを更新しました.. – Baba

関連する問題