php按日期排序文件夹而不是名称

时间:2014-11-13 09:42:07

标签: php arrays sorting directory

目前我的脚本按名称列出所有文件夹。 现在我想按照他们创建的日期对其进行排序。

Index.php:

<?php
include("include.php");
include("header.php");
?>

<h1>Photo</h1>
<p class="breadcrumb"><a href="/">home</a> &gt; gallery</p>

<?php if(count($categories_array)<=0){?>
<p>There are no photo categories, create one or more categories before uploading photos</p>
<?php } 
if(count($categories_array)>0){?>

<div>   
  <?php foreach($categories_array as $photo_category=>$photos_array){?> 

      <?php  
      $category_thumbnail = $gallery_url."/layout/pixel.gif"; 
      if(file_exists('files/'.$photo_category.'/thumbnail.jpg')){ 
          $category_thumbnail = $gallery_url.'/'.$photo_category.'/thumbnail.jpg'; 
     } 

     $category_url = $gallery_url.'/'.$photo_category; 
     ?> 

       <span class="category_thumbnail_span" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height+20;?>px;"> 
     <a class="category_thumbnail_image" href="<?php echo $category_url;?>" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height;?>px; background-image:url('<?php echo $gallery_url;?>/layout/lens_48x48.png');" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> 
     <img src="<?php echo $category_thumbnail;?>" width="<?php echo $settings_thumbnail_width;?>" height="<?php echo $settings_thumbnail_height;?>" alt="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>" /> 
     </a> 
     <a class="category_thumbnail_title" href="<?php echo $category_url;?>" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> 
     <?php echo htmlentities(str_replace('-',' ', truncate_by_letters($photo_category, 16, '..')), ENT_QUOTES, "UTF-8");?> (<?php echo count($photos_array);?>) 
     </a> 
     </span> 


<?php } ?> 

</div>

include.php:

<?php   
include("settings.php");
$page_load_start = microtime(true);
 if (!isset($_SESSION)) {
session_start();
 }

setlocale(LC_CTYPE, "en_US.UTF-8");  
$gallery_domain = str_replace("www.", "", $_SERVER['HTTP_HOST']);  
$gallery_url  = dirname($_SERVER['SCRIPT_NAME']);
$gallery_url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $gallery_url);

error_reporting(E_ALL ^ E_NOTICE);

include("system_functions.php");  
$is_admin = false;

  if($_SESSION['session_admin'] == md5($_SESSION['session_secret'].$settings_secret)){
$is_admin = true;
  }

 $categories_array = array();  
 $timer_1 = microtime(true);



 // loop over files directory and read the categories
$scandir_array = scandir('files');
foreach($scandir_array as $folder){

if(is_dir('files/'.$folder) and $folder!='.' and $folder!='..'){

    // define this key in the array, it will be blank, store categories as keys
    $categories_array[$folder] = array(filectime($folder));                

    // $total_photos_array[$folder] = 0;

    $files_in_dir = scandir('files/'.$folder);
    foreach($files_in_dir as $file){                       
        if($file!='.' and $file!='..'){             
            // if file is not the category thumbnail (thumbnail.jpg) and not _thumb.jpg and not _small.jpg
            if($file != "thumbnail.jpg" and substr($file, strlen($file)-10) != "_small.jpg" and substr($file, strlen($file)-10) != "_thumb.jpg"){
                // $total_photos_array[$folder]++;
                $base_file_name = substr($file, 0,  strlen($file)-4);                                       
                // insert this file in the array of files
                array_push($categories_array[$folder], $base_file_name);

                //echo "<br>$base_file_name";

            }
        }
    }

}
}



$timer_2 = microtime(true);

// !! if you use wrong sorting parameter it will convert the category string keys into integer
arsort($categories_array);

//ksort($categories_array); 

 ?>

我试过$ base_file_name = filemtime($ file);但似乎没有用。

在这方面的任何帮助将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:1)

你可以编写函数,类似于此。它没有经过测试,所以它可以立即起作用。

$scandir_array = order_by_date(scandir('files'));

function order_by_date($files) {

    $return = array();    
    foreach ($files as $file) {
        $return[$file] = filemtime($file);
    }

    arsort($files);
    $return = array_keys($files);

    return $return;
}

答案 1 :(得分:1)

解决:

$categories_array = array (); 
$temp_array = array (); 
$timer_1 = microtime ( true ); 

$scandir_array = scandir ( 'files' ); 
foreach ( $scandir_array as $folder ) { 
if (is_dir ( 'files/' . $folder ) and $folder != '.' and $folder != '..') { 
$timestamp = filemtime ( 'files/' . $folder ); 
$temp_array[$timestamp] = $folder; 
} 
} 

krsort ( $temp_array ); // sorts an array by key. 

foreach ( $temp_array as $folder ) { 
// define this key in the array, it will be blank, store categories as keys 
$categories_array [$folder] = array (); 
$files_in_dir = scandir ( 'files/' . $folder ); 
foreach ( $files_in_dir as $file ) { 
if ($file != '.' and $file != '..') { 
// if file is not the category thumbnail (thumbnail.jpg) and not _thumb.jpg and not _small.jpg 
if ($file != "thumbnail.jpg" and substr ( $file, strlen ( $file ) - 10 ) != "_small.jpg" and substr ( $file, strlen ( $file ) - 10 ) != "_thumb.jpg") { 
$base_file_name = substr ( $file, 0, strlen ( $file ) - 4 ); 
// insert this file in the array of files 
array_push ( $categories_array [$folder], $base_file_name ); 
} // if 
} // if 
} // foreach 
} // foreach