Retrieved list of files/dir how to display alphabetically...
02-09-2008, 03:49 PM
|
Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Hey all,
okay i got my simple file management script going and here the chunk which retrieves all the files and directories and makes my thing...
PHP Code:
function file_extension($file) { $path_info = pathinfo($file); return $path_info['extension']; } // This is the directory to list files for. if(isset($_GET['dir'])) { $theDirectory = DOC_ROOT."/".$_GET['dir'].'/'; } else { $theDirectory = DOC_ROOT."/"; } // Do you want to show directories? change to false to hide directories. $listDirectories = true; if(is_dir($theDirectory)) { echo "<table border='1'><tr><td>Name</td><td>Type</td><td>Size</td><td>CMOD</td><td>ext</td><td>Edit</td></tr>"; echo '<tr><td><a href="file_manager.php">Home</a></td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>'; $dir = opendir($theDirectory); while(false !== ($file = readdir($dir))) { $type = filetype($theDirectory ."/". $file); if($listDirectories || $type != "dir") { if(isset($_GET['dir'])) { $cur_dir = $_GET['dir'].'/'; } echo "<tr><td><a href='?dir=".$cur_dir.$file."'>" . $file . "</a></td>"; echo "<td>" . $type . "</td>"; echo "<td>"; echo filesize($theDirectory.$file); echo "</td><td>"; echo substr(sprintf('%o', fileperms($theDirectory.$file)), -4); echo "</td><td>"; if($type == "file") { echo file_extension($file); echo '</td>'; $editables = array('php','txt','phps','htm','html','js','css','htaccess'); foreach($editables as $allowed_ext) { if(file_extension($cur_dir.$file) == $allowed_ext) { echo '<td>'; echo '<a href="file_manager.php?do=edit&file='.$cur_dir.$file.'">Edit</a>'; echo '</td>'; } }// Emd Foreach }//End if file is file. else { echo '-</td><td>-</td>';//Shows in Edit collum and EXT }//End else file echo '</tr>'; } }// End while. closedir($dir); echo "</table>"; } else { echo $theDirectory . " is not a directory"; }
but how can i make the files show alphabetically (and if possible have directories at the top and files after them, like in most OS's)
Thanks al, and as always TP if you can help 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-09-2008, 04:48 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 843
Name: Mike
Location: United Kingdom
|
I think an array sort is what you need:
http://uk.php.net/manual/en/function...-multisort.php
http://uk.php.net/manual/en/function.asort.php
Though, I'm afraid I can only guess you need to put it about :
PHP Code:
$dir = opendir($theDirectory); while(false !== ($file = readdir($dir)))
For example:
PHP Code:
$dir = opendir($theDirectory); asort($dir); while(false !== ($file = readdir($dir)))
__________________
My Blog/Site: Please login or register to view this content. Registration is FREE
Last edited by rogem002; 02-09-2008 at 04:53 PM..
|
|
|
|
02-09-2008, 04:57 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
had a go with it and it seems that it isnt a array
i used:
PHP Code:
while(false !== array_multisort(($file = readdir($dir)),SORT_DESC,SORT_STRING))
and i got the error: Warning: array_multisort() [ function.array-multisort]: Argument #1 is expected to be an array or a sort flag in /home/ukcalm/public_html/admin/file_manager.php on line 69
So erm any idea?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-10-2008, 01:47 AM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Store all of your strings in an array and then sort that array.
|
|
|
|
02-10-2008, 05:27 AM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
i tried that using this block:
PHP Code:
$dir = opendir($theDirectory); while (false !== ($dir_files = readdir($dir))) { $files[] = $dir_files; } while(array_multisort($files,SORT_DESC,SORT_STRING)) { $type = filetype($theDirectory ."/". $file); if($listDirectories || $type != "dir") { if(isset($_GET['dir'])) { $cur_dir = $_GET['dir'].'/';
but now i cant load the page, it takes ages uses up a TON of memory and CPU for ie and then crashes and freezes the system
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-10-2008, 12:55 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 115
Name: Sharon
Location: Leicester, uk
|
hello!
don't know if this would help?
http://www.tizag.com/mysqlTutorial/mysqlorderby.php
i am a newbie so just trying to help
shaz x
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
|
|
|
|
02-10-2008, 01:36 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
nope thats SQL, i know how to do that 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-10-2008, 01:42 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 115
Name: Sharon
Location: Leicester, uk
|
Quote:
Originally Posted by dansgalaxy
nope thats SQL, i know how to do that 
|
oops, sorry, i just seen the word sort 
Shaz x
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
|
|
|
|
02-10-2008, 02:09 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 219
Name: Rob
Location: UK
|
You should be using the new scandir function (php5). http://uk3.php.net/scandir
Scandir returns an array not a string. You can then use the sort($array) functions http://uk2.php.net/sort.
Then you can loop through the array with a foreach loop.
|
|
|
|
02-10-2008, 05:33 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Thanks Maxxximus works a treat, if only i could put the folders before the normal files this would be perfect
P.S i tried but cant give u no more TP 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-12-2008, 11:09 AM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Just downloading it, looks ok, dont seem to have any other functins than display but hopefully i can figure out how to seperate the folders from the files from the code 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-12-2008, 11:14 AM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
wow theres alot of of stuff in that script, seems way too overly complicated.
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
02-12-2008, 01:39 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 209
Name: Darran
|
Quote:
Originally Posted by dansgalaxy
wow theres alot of of stuff in that script, seems way too overly complicated.
|
I'm still very much a newbie where PHP is concerned and have no problem using other (OS) scripts to do the job ...
that said yes theres a lot of un-needed and repeated stuff in there ... but when I just want a pretty directory listing it does the job ... complete with pretty icon which my downloaders seem to like
Starlord
|
|
|
|
02-12-2008, 02:02 PM
|
Re: Retrieved list of files/dir how to display alphabetically...
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Lol, im not saying its not good, im just going for as simple as possible and just non pretty and functional  have most of the file manager done
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
|
« Reply to Retrieved list of files/dir how to display alphabetically...
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|