Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 库缺少目录中的文件-如何从目录中检索所有文件_Php_Javascript_Jquery_Fancybox_Gallery - Fatal编程技术网

Php 库缺少目录中的文件-如何从目录中检索所有文件

Php 库缺少目录中的文件-如何从目录中检索所有文件,php,javascript,jquery,fancybox,gallery,Php,Javascript,Jquery,Fancybox,Gallery,在使用fancybox时,我终于获得了从目录填充图库的代码。这是一个基本的缩略图库,单击后显示较大的图像。唯一的问题是它缺少缩略图目录中的许多文件 该代码检索较大图像的所有链接,但不会检索所有缩略图,只有少数缩略图,而且它们甚至不符合顺序 我的代码有什么问题 <?php $directory = 'thumb'; //where the gallery thumbnail images are located $allowed_types=array('jpg','jpeg','gif

在使用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;


这工作做得很好!如果我有15个代表点,我会完全给你的答案a+1。非常感谢你!