0

ディレクトリからの画像の表示に関するこれらのビデオを見たところですが、コードを変更するための助けが必要です。

http://www.youtube.com/watch?v=dHq1MNnhSzU - パート 1

http://www.youtube.com/watch?v=aL-tOG8zGcQ - パート 2

ビデオが示しているものは、私が望んでいたものとほぼ同じですが、私が考えているシステムはフォト ギャラリー用です。

ギャラリーと呼ばれるフォルダーを作成する予定です。このフォルダーには、異なる写真セットごとに 1 つずつ、他のフォルダーが含まれます。

  • ギャラリー
    • アルバム 1
    • アルバム 2

1 ページにディレクトリのみを識別して表示できるように、コードを変更する方法を教えてください。そうすれば、これらのディレクトリをアルバム自体に移動するリンクに変換し、元のコードを使用してそこから画像を取得できます。

ビデオコードが必要な方はこちら

$dir = 'galleries';
$file_display = array('bmp', 'gif', 'jpg', 'jpeg', 'png');

if (file_exists($dir) == false) {
echo 'Directory \'', $dir , '\' not found!';
} else {
$dir_contents = scandir($dir);

foreach ($dir_contents as $file) {
    $file_type = strtolower(end(explode('.', $file)));

    if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
        echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
    }
}
}
4

2 に答える 2

0

すべてのディレクトリを一覧表示するには、次のような関数を使用する必要があります。

function getDirectory( $path = '.', $level = 0 ){ 

$ignore = array( 'cgi-bin', '.', '..' ); 
// Directories to ignore when listing output. Many hosts 
// will deny PHP access to the cgi-bin. 

$dh = @opendir( $path ); 
// Open the directory to the handle $dh 

while( false !== ( $file = readdir( $dh ) ) ){ 
// Loop through the directory 

    if( !in_array( $file, $ignore ) ){ 
    // Check that this file is not to be ignored 

        $spaces = str_repeat( '&nbsp;', ( $level * 4 ) ); 
        // Just to add spacing to the list, to better 
        // show the directory tree. 

        if( is_dir( "$path/$file" ) ){ 
        // Its a directory, so we need to keep reading down... 

            echo "<strong>$spaces $file</strong><br />"; 
            getDirectory( "$path/$file", ($level+1) ); 
            // Re-call this same function but on a new directory. 
            // this is what makes function recursive. 

        } else { 

            echo "$spaces $file<br />"; 
            // Just print out the filename 

        } 

    } 

} 

closedir( $dh ); 
// Close the directory handle 

}

次に、ユーザーが選択したディレクトリを $dir 変数として、現在持っている関数に渡します。

于 2013-02-05T18:23:38.660 に答える
0

現在、コードをテストすることはできませんが、次の行に沿ってここで解決策を見たいと思っています:

$directory = new RecursiveDirectoryIterator('path/galleries');
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.(bmp|gif|jpg|jpeg|png)$/i', RecursiveRegexIterator::GET_MATCH);                

SPL は強力で、もっと使用する必要があります。

RecursiveDirectoryIteratorは、ファイルシステム ディレクトリを再帰的に反復するためのインターフェイスを提供します。 http://www.php.net/manual/en/class.recursivedirectoryiterator.php

于 2013-02-05T18:28:29.563 に答える