Sorting by file name is out of order when it reaches 10

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
digi-what
Forum Newbie
Posts: 4
Joined: Sat Nov 01, 2008 8:23 am

Sorting by file name is out of order when it reaches 10

Post by digi-what »

I made an array that renames newly uploaded files to 1 2 3 4 and so on, and then sorts them on the page, however file names like 10 or 1 are not in order.

How can i make the file name 01 02 03 04?

If I try to ad a 0 when it counts up, it doesnt work.

if (file_exists("blah/".$file.".tif"))
{
$file=$file + '01';
}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Sorting by file name is out of order when it reaches 10

Post by onion2k »

Read the manual page for the sort() function. There are different options that control how it sorts things. http://uk3.php.net/sort
digi-what
Forum Newbie
Posts: 4
Joined: Sat Nov 01, 2008 8:23 am

Re: Sorting by file name is out of order when it reaches 10

Post by digi-what »

I have looked at that page many times. I have used sort() and others that are listed on that page and still file names with 10 or 1 are out of order. i need to automatically insert a 0 before the number so it sorts correctly. My question is how to do this?

example: 1.jpg 2.jpg etc etc becomes 01.jpg 02.jpg etc
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: Sorting by file name is out of order when it reaches 10

Post by Hannes2k »

Hi,
without showing use your code for sorting the filenames you get, no one can helps you. We are not visionaries here.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Sorting by file name is out of order when it reaches 10

Post by Ziq »

If you want to sort files you can use a natsort() function.

However if you want to add '0' in head of your string you can use a concatenation operation.

Code: Select all

 
<?
//...
$file = '0'.$file;
//...
?>
 
and then sort data.
Post Reply