Current location: Hot Scripts Forums » Programming Languages » PHP » Sorting opendir function


Sorting opendir function

Reply
  #1 (permalink)  
Old 05-26-08, 02:34 PM
lindasnephew lindasnephew is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Sorting opendir function

Hi, i have found the following snippet of code which i am modify make part of an image gallery script i am making, the trouble i'm having is the way it's sorting files.

At the minute the following code will just produce a list of all the files found in the directory.

PHP Code:

<?


//define the path as relative
$path "/home/httpd/vhosts/spoono.com/httpdocs";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo 
"Directory Listing of $path<br/>";

//running the while loop
while ($file readdir($dir_handle)) 
{
   if(
$file!="." && $file!="..")
      echo 
"$file<p>";
}

//closing the directory
closedir($dir_handle);

?>
the problem is the way it handles filenames with numbers in.. i.e

if i have the following files

image1.jpg
image2.jpg
image11.jpg
image19.jpg
image21.jpg

it doesn't display in that order it does it in this order

image1.jpg
image11.jpg
image19.jpg
image2.jpg
image21.jpg

does anybody have any idea how to fix this so it lists how it should? im planning on just numbering my images like that to order them and it'd be a pain if i have to take that in to account when numbering them..

thanks for any help
Reply With Quote
  #2 (permalink)  
Old 05-26-08, 03:33 PM
jstanden jstanden is offline
Newbie Coder
 
Join Date: Oct 2003
Location: Orange County, California
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
The OS isn't always going to return a sorted list of files in a directory. You could use glob() (http://us2.php.net/glob) for that.

If you have control over the filenames, it also helps to name them with zero-padding, like:
image-00000001.jpg
image-00000010.jpg
image-00000100.jpg
image-00001000.jpg
__________________
Jeff Standen, Chief of R&D, WebGroup Media LLC.
http://www.cerb4.com/ - Cerberus Helpdesk 4.2
Reply With Quote
  #3 (permalink)  
Old 05-26-08, 03:54 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
As long as you don't change the way you are naming your image files, then this code will sort them in the proper order.
This code allows up to 999 files.
If you need room for more than that, then I will show you how to modify the code to accept more.

PHP Code:

<?php
//define the path as relative
$path "/home/httpd/vhosts/spoono.com/httpdocs";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
//running the while loop
$ct=0;
while (
$file readdir($dir_handle))
{
   if(
$file!="." && $file!="..")
   {
    
$temp[0][$ct]=substr($file,5,3)?substr($file,5,3):"";
    
$temp[1][$ct]=$file;
    if(
substr($temp[0][$ct],1,1)=="."){$temp[0][$ct]="00".substr($temp[0][$ct],0,1);}
    if(
substr($temp[0][$ct],2,1)=="."){$temp[0][$ct]="0".substr($temp[0][$ct],0,2);}
    
$ct++;
    }
}
//closing the directory
closedir($dir_handle);
echo 
"Directory Listing of $path<br/>";
// Sort dir_names
array_multisort($temp[0],$temp[1]);
for(
$i=0;$i<count($temp[0]);$i++)
{
 echo 
$temp[1][$i]."<p>";
 }
?>
__________________
Jerry Broughton

Last edited by job0107; 05-26-08 at 03:58 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[2005] StartPosition for CommonDialogs? tim8w Windows .NET Programming 10 01-08-09 03:39 AM
Parsing XML dodotopia PHP 1 02-08-06 03:51 PM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 07:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.