LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2012, 06:20 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 373

Rep: Reputation: 6
[PHP] I need help sorting directories on my forum.


Ok, I have this forum that I sort directories with. The code below is what I use to sort the directories
and I believe it's actually array_multisort that actually does the sorting. It worked up until Someone posted to my forum which uses the directories to hold all the posts for each topic.
I have one way of solving this problem but I was hoping if someone might show me a method for making a directory list sorted by the date that the directories where created and not by the date they where modified by.

If you want to see what I need help with go to o0oo0.net16.net/forum/

Code:
<?php
// open this directory
$myDirectory = opendir("SCIENCE");
// get each entry
while($entryName = readdir($myDirectory))
{
    $dirArray[] = './SCIENCE/' . $entryName;
}
// close directory
closedir($myDirectory);
//	count elements in array
$indexCount	= count($dirArray);
array_multisort(array_map('filemtime', $dirArray), SORT_ASC, $dirArray);
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) 
{
    $file = basename($dirArray[$index]);

    if (substr($file, 0, 1) != "."
        && $file != "index.php"
        && $file != "new_topic.html")
    {
        print("<br>&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"$dirArray[$index]\">$file</a>&nbsp;");
        print("is a ".filetype($dirArray[$index]).",&nbsp;");
        print("its size is ".filesize($dirArray[$index])." byte");
        if(filesize($dirArray[$index]) != 1)
            print("s");
    }
}
?>
 
Old 03-30-2012, 09:28 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by errigour View Post
I was hoping if someone might show me a method for making a directory list sorted by the date that the directories where created and not by the date they where modified by.
Unfortunately most filesystems do not retain that timestamp at all. And, as you've noticed, the mtime timestamp is updated for the directory whenever a file is created, deleted, or renamed in that directory.

You could use the directory status change timestamp, though. It is only updated when the directory access mode, owner, group, or other ("inode") attributes are changed. Operations on files in that directory do not affect that timestamp at all.

For this change, just replace 'filemtime' with 'filectime'.

An alternative option is to create a hidden file, say .created in each directory, and use its timestamp instead. In that case you would use a helper function, say 'timecreated' instead of 'filemtime':
PHP Code:
function timecreated($directory)
{
    
$result = @filemtime($directory '/.created');
    if (
$result === FALSE)
        
$result = @filemtime($directory);
    return 
$result;

This version uses the modification timestamp of the .created file if it exists; if not, it uses the modification timestamp of the specified directory itself. This way you don't need to worry about races when creating new directories.

The @ in the filemtime() calls just suppresses all errors and warnings related to that specific call, even if you have warnings enabled.

Hope this helps,
 
Old 03-31-2012, 08:17 AM   #3
errigour
Member
 
Registered: May 2009
Posts: 373

Original Poster
Rep: Reputation: 6
Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(PHP, XML) need help sorting through a multi-dimensional array gatorpower Programming 1 09-17-2010 12:34 PM
PHP fgetcsv sorting phpnewb72 Programming 1 12-17-2007 07:37 PM
sorting through large file directories n_hendrick Linux - General 4 05-08-2007 12:08 PM
I need help sorting an array in PHP! socceroos Programming 14 05-09-2006 02:37 AM
Sorting Amarok Collection into directories nicholas.perkins Linux - Software 3 11-09-2005 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration