Jump to content

[SOLVED] Directory List Script


redhat617

Recommended Posts

I have a script on our website at: http://www.silverlakefire.com/minutes.php that looks in a certain directory and builds a page that lists all the files in said directory. The script works fine except as you can see on the page, I can't seem to get them in any sort order. I would like them to sort by filename in descending order. Does anyone have any ideas what I need to fix in my script. I have a copy of it below:

Thank You...

 

<?php

define("BASE_DIR", './data/minutes/');

$imgsrc="exts/";

?>

<?php

$dir=str_replace('..','',$_GET['dir']);

if(substr($dir,-1,1)!='/'&&$dir!='') $dir.='/';

 

function prevDir($dir) {

if (substr($dir, -1, 1) == '/')

$dir = substr($dir, 0, strlen($dir)-1);

if (($pos=strrpos($dir, '/')) !== false)

return substr($dir, 0, $pos+1);

else

return '';

}

 

$row=0;

$suffixes=array("","KB","MB","GB","TB","PB");

 

echo "\t</div>\n";

echo "\t<div>\n";

echo "\t\t<table cellspacing=\"0\">\n";

echo "\t\t\t<tr class=\"title\">\n";

echo "\t\t\t\t<td style=\"width:20px;\"> </td>\n";

echo "\t\t\t</tr>\n";

echo "\t\t\t<tr class=\"row".($row%2)."\">\n";

echo "\t\t\t</tr>";

 

$row++;

 

$dirs = array();

$files = array();

$totalsize = 0;

if ($dirlink = @opendir(BASE_DIR.$dir)) {

while (($entry = readdir($dirlink)) !== false) {

if ($entry != "." && $entry != "..") {

if (is_dir(BASE_DIR."{$dir}/{$entry}")) {

$dirs[$entry]['time'] = filemtime(BASE_DIR."{$dir}/{$entry}");

$dircount=0;

if ($sublink = @opendir(BASE_DIR."{$dir}/{$entry}")) {

while (($current = readdir($sublink)) !== false) {

if ($current != "." && $current != "..") {

$dircount++;

}

}

closedir($sublink);

}

else

$dircount = "?";

$dirs[$entry]['size'] = $dircount;

}

else {

$files[$entry]['time'] = filemtime(BASE_DIR."{$dir}/{$entry}");

$size = filesize(BASE_DIR."{$dir}/{$entry}");

$totalsize += $size;

$i = 0;

while($size > 1024) {

$size /= 1024;

$i++;

}

$files[$entry]['size'] = number_format($size,2)." ".$suffixes[$i];

}

}

}

closedir($dirlink);

}

$totalcount = count($dirs) + count($files);

$i=0;

while($totalsize > 1024) {

$totalsize /= 1024;

$i++;

}

$totalsize = number_format($totalsize, 2)." ".$suffixes[$i];

 

foreach($dirs as $key=>$value){

echo "<tr class=\"row".($row%2)."\">\n";

echo "\t\t\t\t<td><img src=\"".$imgsrc."folder.gif\" alt=\"Directory\" /></td>\n";

echo "\t\t\t\t<td><a href=\"?dir={$dir}{$key}\">{$key}</a></td>\n";

echo "\t\t\t\t<td class=\"right\">".$value['size']." files</td>\n";

echo "\t\t\t\t<td class=\"right\">".date("d-M-Y H:i",$value['time'])."</td>\n";

echo "\t\t\t</tr>";

$row++;

}

 

foreach($files as $key=>$value){

$ext=str_replace(".","",strrchr($key,"."));

echo "<tr class=\"row".($row%2)."\">\n";

echo "\t\t\t\t<td>";

 

if(file_exists($imgsrc.$ext.".gif"))

echo "<img src=\"{$imgsrc}{$ext}.gif\" alt=\"{$ext}\" />";

else

echo "<img src=\"{$imgsrc}unknown.gif\" alt=\"{$ext}\" />";

 

echo "</td>\n";

echo "\t\t\t\t<td><a target=_blank href=\"".BASE_DIR.$dir.$key."\">{$key}</a></td>\n";

echo "\t\t\t</tr>";

$row++;

}

 

echo "<tr class=\"title\">\n";

echo "\t\t\t</tr>\n";

echo "\t\t</table>\n";

?>

 

Link to comment
Share on other sites

I have tried the rsort. Here is the syntax I used it:

rsort($files);

foreach($files as $key=>$val){
$ext=str_replace(".","",strrchr($key,"."));
echo "<tr class=\"row".($row%2)."\">\n";
echo "\t\t\t\t<td>";

if(file_exists($imgsrc.$ext.".gif"))
	echo "<img src=\"{$imgsrc}{$ext}.gif\" alt=\"{$ext}\" />";
else
	echo "<img src=\"{$imgsrc}unknown.gif\" alt=\"{$ext}\" />";

echo "</td>\n";
echo "\t\t\t\t<td><a target=_blank href=\"".BASE_DIR.$dir.$key."\">{$val}</a></td>\n";
echo "\t\t\t</tr>";
$row++;
}

echo "<tr class=\"title\">\n";
echo "\t\t\t</tr>\n";
echo "\t\t</table>\n";

 

When I do that, the output in the webpage is:

Array

Array

Array

 

Instead of the filename.

Link to comment
Share on other sites

  • 1 month later...

<?php
$x = glob("*.php");
sort($x);
for ($i=0;$i<count($x);$i++) {
echo $x[$i]. "<br/>";
}
?>

 

That works for me, installed in the target directory.  You can adjust to suit, including turning the file names into clickable links.

Link to comment
Share on other sites

Well I am almost there, and thanks to all for your help in getting me here. Now I just am struggling in getting the links to display right. When I use Andy B's code it works fine, when I try to get them hyperlinked it is different.

 

If i use the following code:

<center>
<?php
chdir('./data/news/');
$x = glob("*.*"); 
rsort($x);
for ($i=0;$i<count($x);$i++) {
echo "<p><a target=_blank href=\"/data/news/\"".$x.">$x</a></p>\n"; 	
#	echo $x[$i]. "<br/>";
}
?>

All I get on the page is ARRAY, ARRAY, ARRAY,....

 

If I use the code:

<center>
<?php
chdir('./data/news/');
$x = glob("*.*"); 
rsort($x);
for ($i=0;$i<count($x);$i++) {
echo "<p><a target=_blank href=\"/data/news/\"".$i.">$i</a></p>\n"; 	
#	echo $x[$i]. "<br/>";
}
?>

All i get is 0,1,2,3.....

 

Any suggestions?

 

Thank You

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.