Results 1 to 7 of 7

Thread: Sort filenames alphabetically

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Sort filenames alphabetically

    Hello everybody,

    Here's my code to scan a particular directory and show all files with a checkbox next to each filename. My client just want to show these filenames sorted alphabetically.

    Code:
    $dir = "../download/";
    $i=0;
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
        print "<tr><td height='25'></td></tr>";
            while (($file = readdir($dh)) != false) {
                // print "filename: $file : filetype: " . filetype($dir . $file) . "\n";
                if(filetype($dir . $file)!='dir' and !in_array($file,$att))
                {
    				$i++;
    				//echo "";
    				print "<tr><td class='caption'>" . "<a href='$dir$file'>" . $file . "</td>";
    				if($_GET[id]=="admin")
    					print"<td class='caption'><input type='checkbox' name='chk$i' value='$file'></td>";
    				else
    					print"<td class='caption'></td>";
    			};
            }
        closedir($dh);
        }
    }
    Thanks.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Sort filenames alphabetically

    When you have all the file names in an array, then use the sort function:

    http://ch2.php.net/sort


    - ØØ -

  3. #3

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Sort filenames alphabetically

    Can you tell me how to create a dynamic array in PHP as I don't know how many files will be present in folder.

    Thanks.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Sort filenames alphabetically

    All arrays in PHP are dynamic, just addand remove as and when you require.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: Sort filenames alphabetically

    lets work with this:

    Code:
                if(filetype($dir . $file)!='dir' and !in_array($file,$att))
                {
    				$i++;
    				//echo "";
    				print "<tr><td class='caption'>" . "<a href='$dir$file'>" . $file . "</td>";
    				if($_GET[id]=="admin")
    					print"<td class='caption'><input type='checkbox' name='chk$i' value='$file'></td>";
    				else
    					print"<td class='caption'></td>";
    			};
    First of all, I am not sure exactly why the last semicolon is there. so I'm going to take it out of mine.

    Here's an idea of what to do:

    Code:
                if(!isdir($file) && !in_array($file,$att))
                {
    		$filearray[] = $file;
    	    }
    After that you can display them.

  6. #6

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Sort filenames alphabetically

    I populated a two dimensional array with filenames and directory names. Its working fine, I checked by printing them on page. But the sort function is not working well. How can I sort a two dimensional string array with respect to first dimension?

    Thanks.

  7. #7
    Member AWC_Joe's Avatar
    Join Date
    Jan 2006
    Location
    Located
    Posts
    49

    Re: Sort filenames alphabetically

    Maybe: http://us3.php.net/manual/en/functio...-multisort.php

    Which could have been found in NoteMe's link also.


    _
    |
    Some programs and scripts ive made: http://wiki.anotherwebcom.com
    What was once an opinion, became a fact, to be later proven wrong...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width