Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Closed Thread
Old 01-31-2011, 06:35 PM Sort files by NAME
Average Talker

Posts: 17
Trades: 0
Hello,

I have a little code to fetch all the images out of a directory and display them on the page. However I would like to order them ascending image001, image002, image003.

Can anybody help me to achieve this?

PHP Code:
<?php

$dir 
"images/portfolio/architecturephotography/";

if (
$opendir opendir($dir))
{
while ((
$file readdir($opendir)) !== FALSE)
{
if (
$file!="."&&$file!="..")
echo 
"<img src='$dir/$file' /><br />";
}
}
?>
Would be much appreciated.
jamiek is offline
View Public Profile
 
 
Register now for full access!
Old 01-31-2011, 06:47 PM Re: Sort files by NAME
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You're going to have to insert the files into an array and then sort them:
PHP Code:
$files = array();

$dir "images/portfolio/architecturephotography/";

if (
$opendir opendir($dir))
{
     while ((
$file readdir($opendir)) !== FALSE)
     {
          if (
$file!="."&&$file!="..")
               
$files[] = $file;
     }
}

natsort($files);

foreach(
$files as $file)
{
     echo 
"<img src='$dir/$file' /><br />";

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 01-31-2011 at 06:49 PM..
NullPointer is online now
View Public Profile Visit NullPointer's homepage!
 
Old 01-31-2011, 07:02 PM Re: Sort files by NAME
Average Talker

Posts: 17
Trades: 0
Thanks a lot Matt, worked a treat.
jamiek is offline
View Public Profile
 
Old 01-31-2011, 08:54 PM Re: Sort files by NAME
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
Don't forget to close the handle that you opened.

Code:
if ($opendir = opendir($dir))
{
     while (($file = readdir($opendir)) !== FALSE)
     {
          if ($file!="."&&$file!="..")
               $files[] = $file;
     }
     closedir($opendir);
}
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]
RonnieTheDodger is offline
View Public Profile Visit RonnieTheDodger's homepage!
 
Old 02-02-2011, 02:40 PM Re: Sort files by NAME
Average Talker

Posts: 17
Trades: 0
Is this just good practice Ronnie?
jamiek is offline
View Public Profile
 
Old 02-02-2011, 04:04 PM Re: Sort files by NAME
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
Quote:
Originally Posted by jamiek View Post
Is this just good practice Ronnie?
Um, yes.

Technically the opendir() function returns a handle to an object in memory.

You can use that handle to perform other functions, such as readdir($handle).

When you are done with the handle, discard it by closing it. This removes the object from memory.

If not discarded, the object remains in memory. Every call to this part of the code creates another object, which in turn gets added into memory. And so on.

Imagine the page that this portion of code resides on all of a sudden winds up on the front page of Digg. What do you think will happen with a surge of page hits and that handle is not closed properly?
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]
RonnieTheDodger is offline
View Public Profile Visit RonnieTheDodger's homepage!
 
Old 02-02-2011, 04:29 PM Re: Sort files by NAME
Average Talker

Posts: 17
Trades: 0
Ah I see like a memory leak, many thanks.
jamiek is offline
View Public Profile
 
Old 02-02-2011, 05:02 PM Re: Sort files by NAME
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
Quote:
Originally Posted by jamiek View Post
Ah I see like a memory leak, many thanks.
No. Unreleased memory.

And to be more explicit in your code with variable naming:

PHP Code:
if ($dir_handle opendir($dir))
{
     while ((
$file readdir($dir_handle)) !== FALSE)
     {
          if (
$file!="."&&$file!="..")
               
$files[] = $file;
     }
     
closedir($dir_handle);

__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]

Last edited by RonnieTheDodger; 02-02-2011 at 05:05 PM..
RonnieTheDodger is offline
View Public Profile Visit RonnieTheDodger's homepage!
 
Closed Thread     « Reply to Sort files by NAME
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25903 seconds with 12 queries