2012-07-21 74 views
6

很简单folderstructure这样的...PHP&HTML:如何循环显示包含图像的目录?

  • 的index.php
  • IMG
    • someimage1.jpg
    • someimage2.png
    • someimage3.jpg

我想知道它有多困难是使用PHP来阅读这个img文件夹,并创建一个微型网站,通过这个图像与“prev”和“next”链接进行循环。

所以我不想手动指定图像的文件名是什么。我只是想图像添加到文件夹和一个PHP脚本贯穿其中,并产生类似

<a href="nextimage-link" class="next">Next Image</a> 
<a href="previmage-link" class="prev">Previous Image</a> 

导航所以每当我点击“下一步图片”链接它会更新网站,并显示下一个图像。

复杂构建?有关于此的任何想法? 非常感谢您的帮助。

+0

['glob()'](http://il.php.net/manual/en/function.glob.php) – 2012-07-21 11:41:47

+0

或[readdir](http://il.php.net/manual/en /function.readdir.php) – Neograph734 2012-07-21 11:54:31

回答

10

考虑下面,我假设IMG文件夹是在在/ var/WWW文件夹:

$dir = "/var/www/img/*.jpg"; 
//get the list of all files with .jpg extension in the directory and safe it in an array named $images 
$images = glob($dir); 

//extract only the name of the file without the extension and save in an array named $find 
foreach($images as $image): 
    echo "<img src='" . $image . "' />"; 
endforeach; 

为了更好的用户界面,您可以创建图像路径的阵列,并将它们存储在一个JavaScript数组。然后使用“上一个”和“下一个”按钮更改图像阵列的索引,并显示单个标签中的当前值。

+0

我喜欢关于JS Array的想法。我怎样才能创建一个JS数组与PHP? – matt 2012-07-21 12:17:30

+0

您可以使用'realpath()'PHP函数来获取实际路径。 – 2012-07-21 12:18:52

+0

请通过此帮助http://www.w3schools.com/php/func_filesystem_realpath.asp – 2012-07-21 13:01:44