画廊缺少目录中的文件-如何从目录中检索所有文件
Gallery Is Missing Files From Directory- How To Retrieve All Files From Directory
在使用fancybox时,我终于得到了从目录填充库的代码。这是一个基本的缩略图库,可以在单击时显示较大的图像。唯一的问题是它缺少缩略图目录中的许多文件。
该代码检索较大图像的所有链接,但不会检索所有缩略图,只检索其中的一小部分,而且它们甚至没有按顺序排列。
我的代码出了什么问题?
<?php
$directory = 'thumb'; //where the gallery thumbnail images are located
$allowed_types=array('jpg','jpeg','gif','png');//allowed image types
$file_parts=array(); $ext=''; $title=''; $i=0;//try to open the directory
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle)) //traverse through the files
{ if($file=='.' || $file == '..') continue; //skip links to the current and parent directories
$file_parts = explode('.',$file); //split the file name and put each part in an array
$ext = strtolower(array_pop($file_parts)); //the last element is the extension
$title = implode('.',$file_parts); //once the extension has been popped out, all that is left is the filename
$title = htmlspecialchars($title); //make the filename html-safe to prevent potential security issues
natsort($file_parts); //sort by filename--NOT WORKING
$nomargin='';
if(in_array($ext,$allowed_types)) //check if the extension is an allowable type
{
if(($i+1)%4==0) $nomargin='nomargin'; //the last image on the row is assigned the CSS class "nomargin"
//Begin thumbs containers with fancybox class
echo '<div class="thumbs fancybox '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;"> <a rel="group"
href="images/'.$file.'" title="'.$title.'">'.$title.'</a>
</div>';
$i=0; //increment the image counter
} } closedir($dir_handle); //close the directory
?>
您可能想使用的另一种方法来保护缩略图列表:
$directory = 'thumb'; //where the gallery thumbnail images are located
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename
然后简单地进行渲染:
<?php
for($x = 0; $x < count($files); $x++):
$thumb = $files[$x];
$file = basename($thumb);
$nomargin = $x%4 == 0?" nomargin":"";
$title = htmlspecialchars($file);
?>
<div class="thumbs fancybox<?= $nomargin ?>"
style="background:url('<?= $thumb ?>') no-repeat 50% 50%;">
<a rel="group" href="images/'.<?= $file ?>" title="<?= $title ?>"><?= $title ?></a>
</div>
<?php
endfor;
相关文章:
- 将大数组(字符串和类型数组的混合物)存储到blob或文件中/从blob或文件检索大数组的最佳方式
- javascript从选中的复选框中检索输入值,并在同一网页中显示多个html文件
- Javascript-检索文件夹中文件的名称
- 从多个文件上传中检索文件大小
- 尝试从csv文件中检索单个值时出现问题
- html表单,它有文本和表,现在我想在一个简单的文本文件中保存和检索数据,如何实现它
- 为什么可以't我从Json文件中检索数据
- 检索驱动器文件
- 如何从 json 文件中检索所有字段
- 如何通过javascript从state.xml文件中检索所有状态名称
- Chrome应用程序:无法检索文件加载状态
- jQuery动态绑定文件输入并在函数中检索文件
- 当使用AJAX获取PHP文件以从MySQL数据库中检索数据时,会出现下拉表单disapering
- 如何将下拉列表中检索到的值的id从数据库发送到php文件
- 如何从窗口中检索文件.持久存储
- 检索 XML 文件 (JavaScript)
- 问题,用于创建从xml文件检索并以xHTML显示的嵌套列表
- 从外部文件检索JSON数据时,如何访问该数据
- Jquery AJAX($.get或$.ajax) HTML文件检索、操作和JSON发送
- 从css文件检索高度属性时遇到问题