473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

beginner question: directory handling & sorting

I am trying to open a directory on a file server to populate a drop down
menu. I've been able to do this, but the results are not sorted
alphabetically. How would I go about this? I've looked at the php.net
site on directory functions, but haven't found a solution that works.
The sort() function seemed like it should, but it instead returned an
error: sort() expects parameter 1 to be array, resource given.

Any help would be appreciated.
Aug 15 '06 #1
3 1541
Rik
Eric Capps wrote:
I am trying to open a directory on a file server to populate a drop
down menu. I've been able to do this, but the results are not sorted
alphabetically. How would I go about this? I've looked at the php.net
site on directory functions, but haven't found a solution that works.
The sort() function seemed like it should, but it instead returned an
error: sort() expects parameter 1 to be array, resource given.

Any help would be appreciated.
0. you can't sort files, only the list of files.
1. read all the contents of the directory in an array.
2. sort the array

Grtz,
--
Rik Wasmus
Aug 15 '06 #2

Eric Capps wrote:
I am trying to open a directory on a file server to populate a drop down
menu. I've been able to do this, but the results are not sorted
alphabetically. How would I go about this? I've looked at the php.net
site on directory functions, but haven't found a solution that works.
The sort() function seemed like it should, but it instead returned an
error: sort() expects parameter 1 to be array, resource given.

Any help would be appreciated.
glob() returns a list of file paths already sorted. See
http://fi.php.net/glob/. The elements will include the directory if one
is specified in the pattern. Use basename() to strip it out.

Aug 16 '06 #3
This is a homebrewed function I use to keep track of the size of my
code on certain projects. It takes two arguments: $dir is a string,
the path relative to the webserver of the directory you'd like to
parse, and $exclude is an array of top-level folders you don't want
searched. Because of the way PHP handles directory traversing (at
least on Windows boxes), the list comes back absolutely alphabetically
sorted, like this:

/a/b/c/d/e
/a/c/a/a/a
/b/a

function ls( $dir, $exclude ) {
static $i = 0;
$files = array();
$d = opendir( $dir );
while ($file = readdir($d))
{
if ($file == '.' || $file == '..') continue;
if (in_array($file, $exclude) ) continue;
if (is_dir( $dir.'\\'.$file ) and !in_array( $file, $exclude) ) {
$files += ls( $dir.'\\'.$file );
continue;
}
$files[ $dir."\\$file" ][ "lines" ] = count( file( $dir."\\$file" )
);
$files[ $dir."\\$file" ][ "size" ] = filesize( $dir."\\$file" );
$files[ $dir."\\$file" ][ "name" ] = $file;
$path = $dir."\\$file";
$path = str_replace( "\\", "/", $path );
$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $path );
$files[ $dir."\\$file" ][ "path" ] = str_replace( "//", "/", $path );
}
return $files;
}
>From there, it wouldn't be too difficult to work up a solution to sort
by the filename provided by $ls_results[ $iterator ][ 'name' ].

Aug 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++...
9
by: SASRS | last post by:
Here is my code. I am trying to see if I can put a %@ LANGUAGE="VBSCRIPT" %> <% FieldA = Request.QueryString("A") FieldB = Request.QueryString("B") FieldC = Request.QueryString("C") ...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
1
by: Sargas Atum | last post by:
Hi all, 1. I have a problem with cell selection in a table in a DataGrid. I dont want that anybody writes in the cells. That was not a problem I changed them to "read only", but if I am going...
0
by: Eph0nk | last post by:
Hi, I have two small questions concerning databounding an xml file to a datalist. 1. Can i do (ascending/descending) sorting on a certain field? 2. Is it possible to show onliy the first 5...
10
by: Ron Weldy | last post by:
I am working on a website that someone else has set up. They have a web.config file with a customerrors element that points the error handling to a special aspx page. <customErrors...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
3
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.