redhat617 Posted August 30, 2008 Share Posted August 30, 2008 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/ Share on other sites More sharing options...
kratsg Posted August 30, 2008 Share Posted August 30, 2008 Try the following: foreach(sort(glob()) as $filename){ echo $filename."<br>"; } See if that will echo out the filenames alphabetically. glob() is much more useful than an opendir() and you can read up on it. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629535 Share on other sites More sharing options...
redhat617 Posted August 30, 2008 Author Share Posted August 30, 2008 I am very new to PHP, so not to sound dumb, but can you tell me what lines I replace using the suggested code? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629541 Share on other sites More sharing options...
redhat617 Posted August 30, 2008 Author Share Posted August 30, 2008 I did get the glob() script to work. However it also show the directory path and the files are no longer hyperlinks. Here is what it looks like: http://www.silverlakefire.com/newsnew.php Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629560 Share on other sites More sharing options...
JasonLewis Posted August 30, 2008 Share Posted August 30, 2008 To make it just show the filename you can use basename(). To link them just use HTML, along with the filename and path. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629581 Share on other sites More sharing options...
redhat617 Posted August 30, 2008 Author Share Posted August 30, 2008 I have tried to re-write the script using the glob() and basename() still with no results. The original script I posted works fine except I want to sort it in descending order. Any idea how to do that without a total re-write? Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629717 Share on other sites More sharing options...
BlueSkyIS Posted August 30, 2008 Share Posted August 30, 2008 if your items are in arrays, you can use rsort($array) for a reverse sort of the array, then loop over the rsort'ed array, displaying data. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629746 Share on other sites More sharing options...
redhat617 Posted August 30, 2008 Author Share Posted August 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629747 Share on other sites More sharing options...
BlueSkyIS Posted August 30, 2008 Share Posted August 30, 2008 it appears you are sorting an array of arrays, so you may need to echo the part of those sorted arrays that you want, e.g., $val['time'] Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-629753 Share on other sites More sharing options...
redhat617 Posted October 8, 2008 Author Share Posted October 8, 2008 Still having problems with this. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659527 Share on other sites More sharing options...
AndyB Posted October 8, 2008 Share Posted October 8, 2008 It appears as though you have resolved this. If so, click the topic SOLVED icon. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659533 Share on other sites More sharing options...
redhat617 Posted October 8, 2008 Author Share Posted October 8, 2008 No I still have not resolved this. I am back to the original problem. I have tried the Glob function as suggested. I have also tried the Sort funtion but neither give me the required results. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659539 Share on other sites More sharing options...
AndyB Posted October 8, 2008 Share Posted October 8, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659546 Share on other sites More sharing options...
redhat617 Posted October 8, 2008 Author Share Posted October 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659624 Share on other sites More sharing options...
trq Posted October 8, 2008 Share Posted October 8, 2008 Because $x is an array. echo "<p><a target=\"_blank\" href=\"/data/news/{$x[$i]}\">{$x[$i]}</a></p>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659630 Share on other sites More sharing options...
redhat617 Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks for all the help. SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/121959-solved-directory-list-script/#findComment-659662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.