# php sort by folder created date

**URL:** <https://www.phphelp.com/t/php-sort-by-folder-created-date/23653>\
**Category:** Code Snippets\
**Created:** [November 12, 2014, 8:58am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653 "2014-11-12T08:58:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilshan](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dilshan](https://www.phphelp.com/u/dilshan)\
**Post date:** [November 12, 2014, 8:58am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/1 "2014-11-12T08:58:08Z")

</div>

Hi Experts,  
Currently this file display all folder order by name but i want to order by folder created date.

index.php contains :  
[php]

```
<?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 } ?> 

```

[/php]

include file contains:  
[php]  
$categories\_array = array();

$timer\_1 = microtime(true);  
// loop over files directory and read the categories  
if ($handle = opendir(‘files’)) {  
while (false !== ($folder = readdir($handle))) {  
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(); 

        // $total_photos_array[$folder] = 0; 
         
        $files_in_dir = scandir('files/'.$folder); 
        foreach($files_in_dir as $file){ 
            // if file ends in _thumb.jpg 
            if(strpos($file, '_thumb.jpg') === strlen($file)-10){ 
                // $total_photos_array[$folder]++; 
                $base_file_name = substr($file, 0, strlen($file)-10); 
                // insert this file in the array of files 
                array_push($categories_array[$folder], $base_file_name); 
            } 
        } 
         
    } 
} 
closedir($handle); 

```

}  
$timer\_2 = microtime(true);

ksort($categories\_array);

[/php]

---

<div class="post-metadata">

**Author:** ![Redscouse](https://www.phphelp.com/user_avatar/www.phphelp.com/redscouse/32/124_2.png) [@Redscouse](https://www.phphelp.com/u/Redscouse)\
**Post date:** [November 12, 2014, 9:27am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/2 "2014-11-12T09:27:50Z")

</div>

Without being able to run the code i am unable to test however, try changing this:  
[php]$base\_file\_name = substr($file, 0, strlen($file)-10);[/php]

to this  
[php]$base\_file\_name = filemtime($file);[/php]

Give it a whirl an let me know how you get on.  
Red 😉

---

<div class="post-metadata">

**Author:** ![dilshan](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dilshan](https://www.phphelp.com/u/dilshan)\
**Post date:** [November 12, 2014, 9:39am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/3 "2014-11-12T09:39:31Z")

</div>

hi thanks for reply.  
tried but no luck. still same.

---

<div class="post-metadata">

**Author:** ![dilshan](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dilshan](https://www.phphelp.com/u/dilshan)\
**Post date:** [November 12, 2014, 9:43am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/4 "2014-11-12T09:43:46Z")

</div>

here is the complete include file  
[php]

\<?php include("settings.php"); $page\_load\_start = microtime(true); if (!isset($\_SESSION)) { session\_start(); } // this is needed to properly handle the special characters in file names, image resize might fail without this setlocale(LC\_CTYPE, "en\_US.UTF-8"); $gallery\_domain = str\_replace("www.", "", $\_SERVER['HTTP\_HOST']); // the gallery\_url is the url of the gallery script (the folder), relative to site root, e.g /gallery $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"); // test if this user is admin or not $is\_admin = false; if($\_SESSION['session\_admin'] == md5($\_SESSION['session\_secret'].$settings\_secret)){ $is\_admin = true; } // $photo\_categories\_array = array(); // $total\_photos\_array = array(); // this will be an array in array, first key is cat, second key is image file: // $categories\_array['cars']['retro-car'] $categories\_array = array(); $timer\_1 = microtime(true); /\* // loop over files directory and read the categories if ($handle = opendir('files')) { while (false !== ($folder = readdir($handle))) { 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(); // $total\_photos\_array[$folder] = 0; $files\_in\_dir = scandir('files/'.$folder); foreach($files\_in\_dir as $file){ // if file ends in \_thumb.jpg if(strpos($file, '\_thumb.jpg') === strlen($file)-10){ // $total\_photos\_array[$folder]++; $base\_file\_name = substr($file, 0, strlen($file)-10); $base\_file\_name = filemtime($file); // insert this file in the array of files array\_push($categories\_array[$folder], $base\_file\_name); } } } } closedir($handle); } \*/ // 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(); // $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); $base\_file\_name = filemtime($file); // insert this file in the array of files array\_push($categories\_array[$folder], $base\_file\_name); //echo "  
$base\_file\_name"; } } } } } $timer\_2 = microtime(true); // !! if you use wrong sorting parameter it will convert the category string keys into integer ksort($categories\_array); //usort($categories\_array); // test if imagemagick is installed $imagemagick\_installed = false; @exec("convert -version", $convert\_output\_array); if(strpos($convert\_output\_array[0], "imagemagick")){ $imagemagick\_installed = true; } ?\>

[/php]

---

<div class="post-metadata">

**Author:** ![dilshan](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dilshan](https://www.phphelp.com/u/dilshan)\
**Post date:** [November 13, 2014, 11:20am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/5 "2014-11-13T11:20:54Z")

</div>

hi guys. any update ?

---

<div class="post-metadata">

**Author:** ![Redscouse](https://www.phphelp.com/user_avatar/www.phphelp.com/redscouse/32/124_2.png) [@Redscouse](https://www.phphelp.com/u/Redscouse)\
**Post date:** [November 13, 2014, 1:21pm UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/6 "2014-11-13T13:21:29Z")

</div>

Thought i posted an update here yesterday, my bad! :-[

I was unable to run the script as the are other includes that fail.

Red 😉

---

<div class="post-metadata">

**Author:** ![dilshan](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dilshan](https://www.phphelp.com/u/dilshan)\
**Post date:** [November 16, 2014, 5:06am UTC](https://www.phphelp.com/t/php-sort-by-folder-created-date/23653/7 "2014-11-16T05:06:06Z")

</div>

here is the files

* * *

[free-php-gallery.zip](https://www.phphelp.com/uploads/default/original/2X/1/18f7c5aec171a0353a90e01a4d9428bea52becf4.zip) (107 KB)
