PHP:image脚本未按字母顺序列出图像

PHP:image脚本未按字母顺序列出图像,php,file,Php,File,我已经修改并清理了其他人编写的这个PHP脚本。在我的WAMP服务器上,它按字母顺序列出了图像(它们都被命名为001.jpg~110.jpg),但是在LiveLamp服务器上,我认为它们是按修改的日期组织的……不管是什么,都不是按文件名。它们都是JPEG图像,所以我不担心按类型排列 那么,我如何修改这个脚本以按字母顺序列出图像呢 function getPictures() { global $page, $per_page, $has_previous, $has_next; if ($h

我已经修改并清理了其他人编写的这个PHP脚本。在我的WAMP服务器上,它按字母顺序列出了图像(它们都被命名为001.jpg~110.jpg),但是在LiveLamp服务器上,我认为它们是按修改的日期组织的……不管是什么,都不是按文件名。它们都是JPEG图像,所以我不担心按类型排列

那么,我如何修改这个脚本以按字母顺序列出图像呢

function getPictures()
{
 global $page, $per_page, $has_previous, $has_next;

 if ($handle = opendir('tour/'))
 {
  $lightbox = rand();
  echo '<ul id="pictures">';

  $count = 0;
  $skip = $page * $per_page;

  if ($skip != 0 ) {$has_previous = true;}

  while ($count < $skip && ($file = readdir($handle)) !== false )
  {
   if (!is_dir($file) && ($type = getPictureType($file)) != '' ) {$count++;}
  }

  $count = 0;

  while ( $count < $per_page && ($file = readdir($handle)) !== false )
  {
   if (!is_dir($file) && ($type = getPictureType($file)) != '' )
   {
    if (!is_dir('thumbs/')) {mkdir('thumbs/');}
    if (!file_exists('thumbs/'.$file)) {makeThumb('tour/'.$file,$type );}

    echo '<li><a href="tour/'.$file.'" rel="lightbox['.$lightbox.']">';
    echo '<img src="thumbs/'.$file.'" alt="" />';
    echo '</a></li>';
    $count++;
   }
  }
  echo '</ul>';

  while (($file = readdir($handle)) !== false)
  {
   if (!is_dir($file) && ($type = getPictureType($file)) != '' )
   {
    $has_next = true;
    break;
   }
  }
 }
}
函数getPictures() { 全局$page,$per_page,$has_previous,$has_next; 如果($handle=opendir('tour/')) { $lightbox=rand(); echo'
    ; $count=0; $skip=$page*$每页; 如果($skip!=0){$has_previous=true;} 而($count<$skip&($file=readdir($handle))!==false) { 如果(!is_dir($file)&($type=getPictureType($file))!=''{$count++;} } $count=0; 而($count<$per_page&&($file=readdir($handle))!==false) { 如果(!is_dir($file)&($type=getPictureType($file))!=“”) { 如果(!is_dir('thumbs/'){mkdir('thumbs/');} 如果(!file_存在('thumbs/'.$file)){makeThumb('tour/'.$file,$type);} 回音“
  • ”; $count++; } } 回声“
”; while(($file=readdir($handle))!==false) { 如果(!is_dir($file)&($type=getPictureType($file))!=“”) { $has_next=true; 打破 } } } }
您可以使用,而不是使用
readdir
,默认情况下按字母顺序排序

默认情况下,排序顺序为字母升序。如果 可选的排序顺序设置为按降序排列,然后 排序顺序是按字母降序排列的。如果设置为 SCANDIR_SORT_NONE则结果未排序

请记住,
scandir
返回一个文件名数组,而
readdir
返回一个条目名

或者,您可以将文件名读入数组,并使用

看起来像一个“lightbox”函数,如果是这样,这里是我在上面发布的函数的完整修改版本

function getPictures()
{
 if ($handle = opendir('tour/'))
 {
  global $page, $per_page, $has_previous, $has_next;
  $lightbox = rand();
  echo '<ul id="pictures">';
  $count = 0;
  $skip = $page * $per_page;

  $file = scandir('tour/');
  $images = array();

  foreach ($file as $key => $value)
  {
   if (!is_dir('tour/'.$value) && ($type = getPictureType('tour/'.$value)) != '' )
   {
    array_push($images,$value);
   }
  }

  natsort($images);

  $count = 0;
  $start = $per_page*$page;
  $end = $start+$per_page - 1;

  foreach ($images as $key => $value)
  {
   if ($key>=$start && $key<=$end)
   {
    echo '<li><a href="tour/'.$value.'" rel="lightbox['.$lightbox.']"><img src="thumbs/'.$value.'" alt="" /></a></li>';
    $count++;
   }
  }
  $not_first = $end+1;
  if ($key>$end) {$has_next = true;}
  if ($not_first!=$per_page) {$has_previous = true;}

  echo '</ul>';
 }
}
函数getPictures() { 如果($handle=opendir('tour/')) { 全局$page,$per_page,$has_previous,$has_next; $lightbox=rand(); echo'
    ; $count=0; $skip=$page*$每页; $file=scandir('tour/'); $images=array(); foreach($key=>$value的文件) { 如果(!is_dir('tour/'.$value)&($type=getPictureType('tour/'.$value))!=“”) { 数组推送($images,$value); } } natsort($图像); $count=0; $start=$per_page*$page; $end=$start+$per_第1页; foreach($key=>$value的图像) { 如果($key>=$start&&$key$end){$has\u next=true;} 如果($not_first!=per_page){$has_previous=true;} 回声“
”; } }
将文件名放入一个数组,对其进行排序,然后输出列表。可能与我正在思考的数组重复,这不是问题的重复。这是清理后的版本?;-)@cbuckley我删除了不必要的空格,将括号放在行首,将括号放在它们完全缺失的地方(!!!!),有一个缺少的isset(实际上我认为是在这个函数之外)…它的格式没有一些PHP那么糟糕,尽管它仍然有点混乱。我在下面发布了一个有效的答案。
function getPictures()
{
 if ($handle = opendir('tour/'))
 {
  global $page, $per_page, $has_previous, $has_next;
  $lightbox = rand();
  echo '<ul id="pictures">';
  $count = 0;
  $skip = $page * $per_page;

  $file = scandir('tour/');
  $images = array();

  foreach ($file as $key => $value)
  {
   if (!is_dir('tour/'.$value) && ($type = getPictureType('tour/'.$value)) != '' )
   {
    array_push($images,$value);
   }
  }

  natsort($images);

  $count = 0;
  $start = $per_page*$page;
  $end = $start+$per_page - 1;

  foreach ($images as $key => $value)
  {
   if ($key>=$start && $key<=$end)
   {
    echo '<li><a href="tour/'.$value.'" rel="lightbox['.$lightbox.']"><img src="thumbs/'.$value.'" alt="" /></a></li>';
    $count++;
   }
  }
  $not_first = $end+1;
  if ($key>$end) {$has_next = true;}
  if ($not_first!=$per_page) {$has_previous = true;}

  echo '</ul>';
 }
}