2016-10-25 4 views
1

私の親ディレクトリにはindex.phpファイルがあり、 "images"という名前のディレクトリに15個の画像があります。私は、ファイル名を読んで、画像をブラウザに表示したい。今は段落タグにファイル名を表示しようとしていますが、何も起こっていません。私はこのスレッドでGetting the names of all files in a directory with PHPPHPを使ってディレクトリからすべてのファイル名を読む

<body> 
<?php 

    $dir = "Challenge8/images"; 

    foreach(glob($dir.'/*.*') as $file) { 
     echo "<p>filename:" . $file . "</p>"; 
    } 
?> 
</body> 

EDITを最初の答えから、このコードを持って:私はFileZillaをに行って、パス全体をコピーし、それが働きました。なぜこれが違いを生み出したのか誰も説明できますか?最後の場所は "/ Challenge8 /画像" まだあった

+1

私はパスを推測が間違っている – nogad

+0

は実際に私にフィードバックを与え、画像の場合だけ – Sidward

+0

をdownvotingないいただきありがとうございます'images /*.*'はフルパスでなければなりません – nogad

答えて

0

ディレクトリ構造:

. 
|-- example 
| `-- list_images.php 
|-- images 
| |-- bar.jpg 
| |-- baz.jpg 
| `-- foo.jpg 
`-- index.php 

list_images.php list_images.phpの

<?php 
$dir = __DIR__ . '/../images'; 
foreach(glob($dir.'/*.*') as $file) { 
    printf("filename:%s\n", $file); 
} 

出力:

filename:/var/www/example/../images/bar.jpg 
filename:/var/www/example/../images/baz.jpg 
filename:/var/www/example/../images/foo.jpg 

。なお、これらはファイルシステムのパスです。あなたのような出力に何かしたいと思う、公的にアクセス可能なパスに上記をオンにする

<img src="/images/<?= basename($file); ?>"> 
関連する問題