Page 1 of 1

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

Posted: Sat Nov 01, 2008 8:30 am
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';
}

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

Posted: Sat Nov 01, 2008 8:51 am
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

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

Posted: Sat Nov 01, 2008 11:33 am
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

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

Posted: Sat Nov 01, 2008 12:08 pm
by Hannes2k
Hi,
without showing use your code for sorting the filenames you get, no one can helps you. We are not visionaries here.

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

Posted: Sat Nov 01, 2008 4:32 pm
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.