Shoutbox

[Solved]PHP directory listing - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: [Solved]PHP directory listing (/showthread.php?tid=55725)

[Solved]PHP directory listing by Mike on 02-11-2006 at 08:15 AM

Hello.

How can I list the contents of a directory using PHP?
I would like the newer files to be shown first.

I have searched google and found this: http://webxadmin.free.fr/article/php-dir-list-and-sort-by-date-209.php
However, I cant get it working...

I set it to list the root ( / ) however, all I get is an empty page.


Thanks!

Edit: I forgot to say, that I'm using PHP 5.0.4


RE: PHP directory listing by Dempsey on 02-11-2006 at 08:23 AM

Well if its not your server, you probably don't have root access, thats why it doesn't work.  Try listing a folder you have access to and see if that works.


RE: PHP directory listing by Mike on 02-11-2006 at 08:32 AM

Well, I'm hosted by dt...
However, Im not really sure what directory I should pass to list....


RE: PHP directory listing by Eljay on 02-11-2006 at 08:34 AM

quote:
Originally posted by Mike
Well, I'm hosted by dt...
However, Im not really sure what directory I should pass to list....

if its current directory you want to list its ./ not / :P
RE: PHP directory listing by Mike on 02-11-2006 at 08:35 AM

quote:
Originally posted by Eljay
quote:
Originally posted by Mike
Well, I'm hosted by dt...
However, Im not really sure what directory I should pass to list....

if its current directory you want to list its ./ not / :P
Just tried, still empty page :(
RE: PHP directory listing by WDZ on 02-11-2006 at 08:38 AM

Well, that code alone doesn't output anything... you need to handle that yourself... :p


RE: PHP directory listing by Eljay on 02-11-2006 at 08:38 AM

quote:
Originally posted by Mike
quote:
Originally posted by Eljay
quote:
Originally posted by Mike
Well, I'm hosted by dt...
However, Im not really sure what directory I should pass to list....

if its current directory you want to list its ./ not / :P
Just tried, still empty page :(

tried print_r($Files); ¬¬

that code is just to get the array

edit: WDZ :(
RE: PHP directory listing by Mike on 02-11-2006 at 08:40 AM

Now i get an output, but it looks weird :P
Something like this:

quote:
Array ( [0] => Array ( [0] => ./mediaplayer.php [1] => 1137315439 )

I guess I will have to loop through the arrays and echo(); them?
RE: PHP directory listing by Eljay on 02-11-2006 at 08:43 AM

quote:
Originally posted by Mike
Now i get an output, but it looks weird :P
Something like this:


I guess I will have to loop through the arrays and echo(); them?

yes
RE: PHP directory listing by WDZ on 02-11-2006 at 08:49 AM

Some example code...

code:
echo "<ul>\r\n";
foreach($Files as $file) {
    $file_path = $file[0];
    $file_name = basename($file_path);
    $last_modified = $file[1];
    $last_modified_str = date('Y-m-d H:i:s', $last_modified);
    echo "<li>$last_modified_str $file_name</li>\r\n";
}
echo "</ul>";
:spam:
RE: PHP directory listing by Mike on 02-11-2006 at 08:57 AM

Thank you WDZ, this seems to work! :)


RE: [Solved]PHP directory listing by Mike on 02-12-2006 at 04:40 PM

Sorry for the double-post, but it seems that Eljay was right on the shoutbox... [Image: msn_sad.gif]

It seems that the code does no sorting at all...
Any way to sort it by date (newest files first)? [Image: msn_tongue.gif]

Thanks [Image: msn_happy.gif]


RE: [Half-Solved]PHP directory listing by WDZ on 02-13-2006 at 05:13 AM

Try replacing the whole DateCmp() function with...

code:
function DateCmp($a, $b)
{
  if($a[1] == $b[1]) return 0;
  return ($a[1] < $b[1]) ? -1 : 1;
}
That should fix the bug. To get descending order, just add this line...
code:
$Files = array_reverse($Files);
...above the foreach() loop. (Yes, that's the lazy way to do it :p)
RE: [Half-Solved]PHP directory listing by Mike on 02-13-2006 at 05:47 AM

That worked, thanks :)