2013-07-02 22 views
1

我終於從我的代碼中使用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 
?> 

回答

0

的另一種方法,你可能需要使用,以確保您的縮略圖列表:

$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; 
+0

這完美地工作!如果我有15個代表點,我會完全給你答案+1。非常感謝! –

相關問題