1

SO の他の場所からの回答を使用して、ネストされた DIR に画像を表示するための基本的な PHP イテレータに取り組んでいます。

私の目標は、PHP を DIR を介して実行し、それが反復するファイルを指す SRC を持つ IMG タグを追加することです。

私はほとんどそこにいますが、画像が表示されない余分な文字が表示されています。

コード(h2 と h3 はデバッグ中の読みやすさのためのものであり、存在するかどうかに関係なく問題が存在します):

// Create recursive dir iterator which skips dot folders
$dir = new RecursiveDirectoryIterator('./images/families/',
    FilesystemIterator::SKIP_DOTS);

// Flatten the recursive iterator, folders come before their files
$it  = new RecursiveIteratorIterator($dir,
    RecursiveIteratorIterator::CHILD_FIRST);

// Maximum depth is 1 level deeper than the base folder
$it->setMaxDepth(5);

// Basic loop displaying different messages based on file or folder
foreach ($it as $fileinfo) {
    if ($fileinfo->isDir()) {
        printf("<h2>Folder - %s\n</h2>", $fileinfo->getFilename());
    } elseif ($fileinfo->isFile()) {
        printf("<h3><img src=\"images/families/%s/%s></h3>", $it->getSubPath(), $fileinfo->getFilename());
    }
}

ブラウザでソースを表示することによる結果:

Folder - smith/40th
<img src="images/families/smith/40th/40th_1.jpg>
<img src="images/families/smith/40th/40th_11.jpg>

..等


ブラウザウィンドウの結果(「新しいウィンドウで画像を開く」を選択した場合):

"The requested URL /images/families/smith/40th/40th_1.jpg><img src= was not found on this server.""

This is the URL in the address bar:
/images/families/smith/40th/40th_1.jpg%3E%3Cimg%20src=

そのため、img を作成する私のコードは、余分な文字を追加したり、ブラウザで正しく読み取れない文字を作成したりしています。

これはエンコードの問題ですか?読んでくれてありがとう。

4

2 に答える 2

1

画像タグを閉じるのを忘れています:

printf("<h2>Folder - %s\n</h2>", $fileinfo->getFilename());
} elseif ($fileinfo->isFile()) {
    printf("<h3><img src=\"images/families/%s/%s\"></h3>", $it->getSubPath(), $fileinfo->getFilename());
}
于 2013-01-30T15:21:04.127 に答える