2012-02-14 236 views
0

我很新的PHP所以我还在学习非常基础,但是我想创建一个图片库。按字母顺序排序由opendir()

经过无数谷歌搜索后,我发现了一个PHP脚本,做什么,我希望它做的,看代码,并稍微操纵它,它与我的网站可以正常使用后,除了图像不是按字母顺序排列的。

这是代码

$max_width = 100; 
$max_height = 100; 
$imagedir = 'gifs/animals/'; //Remember trailing slash 


function getPictureType($ext) { 
    if (preg_match('/jpg|jpeg/i', $ext)) { 
     return 'jpg'; 
    } else if (preg_match('/png/i', $ext)) { 
     return 'png'; 
    } else if (preg_match('/gif/i', $ext)) { 
     return 'gif'; 
    } else { 
     return ''; 
    } 
} 

function getPictures() { 
    global $max_width, $max_height, $imagedir; 
    if ($handle = opendir($imagedir)) { 
     $lightbox = rand(); 
     echo '<ul id="pictures">'; 
     while (($file = readdir($handle)) !== false) { 
      if (!is_dir($file)) { 
       $split = explode($imagedir, $file); 
       $ext = $split[count($split) - 1]; 
       if (($type = getPictureType($ext)) == '') { 
        continue; 
       } 

       $name = substr($file, 0, -4); 
       $title = str_replace("_"," ",$name); 
       echo '<li><a href="'.$name.'">'; 
       echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />'; 
       echo '</a>'; 
       echo ''.$title.''; 
       echo '</li>'; 
      } 
     } 
     echo '</ul>'; 

    } 
} 

我使用这在按字母顺序排序它们的运作SCANDIR()函数,但是我留下了的阵列。然后,我使用implode函数将数组连接在一起,然而之后我仍然不知所措。

任何帮助将不胜感激!

干杯。

回答

1

有什么不对的阵列? 此外,如果您使用pathinfo来获取文件名和扩展名会更好。

$max_width = 100; 
$max_height = 100; 
$imagedir = 'gifs/animals/'; //Remember trailing slash 


function getPictureType($ext) { 
    if (preg_match('/jpg|jpeg/i', $ext)) { 
     return 'jpg'; 
    } else if (preg_match('/png/i', $ext)) { 
     return 'png'; 
    } else if (preg_match('/gif/i', $ext)) { 
     return 'gif'; 
    } else { 
     return ''; 
    } 
} 

function getPictures() { 
    global $max_width, $max_height, $imagedir; 
    if ($files = scandir($imagedir)) { 
     $lightbox = rand(); 
     echo '<ul id="pictures">'; 
     foreach ($files as $file) { 
      $full_path = $imagedir.'/'.$file; 
      if (!is_dir($file)) { 
       $finfo = pathinfo($full_path); 
       $ext = $finfo['extension']; 
       if (($type = getPictureType($ext)) == '') { 
        continue; 
       } 

       $name = $finfo['filename']; 
       $title = str_replace("_"," ",$name); 
       echo '<li><a href="'.$name.'">'; 
       echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />'; 
       echo '</a>'; 
       echo ''.$title.''; 
       echo '</li>'; 
      } 
     } 
     echo '</ul>'; 

    } 
} 
+0

谢谢,这工作。 – Polar 2012-02-14 09:27:43

2

您可以使用​​3210获取某个目录中的文件,按字母顺序排序:

$files = glob('gifs/animals/*.{gif,jpg,png}', GLOB_BRACE); 

遍历文件,使用foreach循环:

foreach($files as $file){ 
    $title = str_replace("_"," ",$file); 
    echo '<li><a href="'.$name.'">'; 
    echo '<img src="thumbs/'.basename($file).'" class="pictures" alt="'.basename($file).'" />'; 
    echo '</a>'; 
    echo ''.$title.''; 
    echo '</li>'; 
} 
+0

这适用于字母排序,我非常感谢您的回复。但是,由于$ file现在输出/gifs/animals/image.gif我无法正确链接到位于gifs/animals/thumbs上的拇指 – Polar 2012-02-14 07:20:22

+0

@Polar:使用'basename()'删除路径,我相应地更新了我的答案。 – konsolenfreddy 2012-02-14 07:42:47