2011-09-12 1 views
0

すべてのファイル名を親ディレクトリのすべてのサブディレクトリに入れたいと思っています。ここでは、コードは次のようになります。前のデータがこのコードにまだ残っているのはなぜですか?

$rep = '.'; 
    if (file_exists($rep)) 
    { 
     $myDirectory = opendir($rep); 

     while($entryName = readdir($myDirectory)) { 
      $dirArray[] = $entryName; 
     } 

     closedir($myDirectory); 

     $indexCount = count($dirArray); 

     sort($dirArray); 

     $resultat = ""; 

     for($index=0; $index < $indexCount; $index++) 
     { 
      if (substr("$dirArray[$index]", 0, 1) != "." && is_dir($dirArray[$index]) && is_numeric($dirArray[$index])) 
      { 
       $resultat .= $dirArray[$index].";";  // dossier de photos d'un client 

       $repClient = $dirArray[$index]; 
       $clientDirectory = opendir($repClient); 
       while($photoName = readdir($clientDirectory)) 
       { 
        $photoArray[] = $photoName; 
       } 
       closedir($clientDirectory); 

       $photoCount = count($photoArray); 

       sort($photoArray); 

       for ($img = 0; $img < $photoCount ; $img++) 
       { 
        if (substr("$photoArray[$img]", 0, 1) != ".") 
        { 
         $resultat .= $photoArray[$img].";"; 
        } 
       } 
       echo "$resultat<br/>"; 
      } 
     } 
    } 

私の問題は、親ディレクトリ内の2つのサブディレクトリが実際に存在していることであり、これらの2つのサブディレクトリのそれぞれは、一つだけのファイル(photo31.pngとphoto32.pngそれぞれを)持っています。

7455573;photo31.png; 
7455573;photo31.png;7455575;photo31.png;photo32.png; 

はなぜphoto31.pngファイルがまだ出力の2行目に入ったさん:私はこのスクリプトを含むWebページを開くと、私はこの出力を得ましたか。

答えて

2

あなたの質問への即座の答えは、$photoArrayの変数をサブディレクトリの繰り返しの間でリセットしないためです。したがって、第2のサブディレクトリを調べると、$photoArrayには、最初のサブディレクトリを調べるときに入力した項目が含まれています。

ここでは、 "結果" の文字列をリセット(for前):

$resultat = ""; 

あなたはまた、$photoArrayをリセットする必要があります。これとは別に

$resultat = ""; 
$photoArray = array(); 

を、全体としてのコードは間違いなく可能性いくつかの改善を使用してください。あなたはこのコードを持っている:

$myDirectory = opendir($rep); 

while($entryName = readdir($myDirectory)) { 
    $dirArray[] = $entryName; 
} 

closedir($myDirectory); 
$indexCount = count($dirArray); 
sort($dirArray); 

をそして、forループ内で、あなたは再びほとんど同じコードを持っている(変数名は変更するが、それは同じです)。このように手動で行うのではなく、自分自身をディレクトリに「入る」ように呼び出す再帰的な実装を試してみてください。

関連する問題