Connecting Tech Pros Worldwide Forums | Help | Site Map

order by file name readdir()

MZ
Guest
 
Posts: n/a
#1: Apr 15 '07
Hello!

I have written such function...

Unfortunately images are not sorted by filename and I don`t know why.

I have such files placed in subfolder:

2005_11_17_koncert_Riverisde_w_Poznaniu_1.jpg
2005_11_17_koncert_Riverisde_w_Poznaniu_2.jpg
2005_11_17_koncert_Riverisde_w_Poznaniu_3.jpg
2005_11_17_koncert_Riverisde_w_Poznaniu_4.jpg
2005_11_17_koncert_Riverisde_w_Poznaniu_5.jpg
2005_11_17_koncert_Riverisde_w_Poznaniu_6.jpg
2006_01_22_wizyta_Panstwa_Rakowskich.jpg
2006_01_22_wizyta_u_babci_Ali.jpg

Even if I replaced the file names into

a2005_11_17_koncert_Riverisde_w_Poznaniu_1.jpg
a2005_11_17_koncert_Riverisde_w_Poznaniu_2.jpg
a2005_11_17_koncert_Riverisde_w_Poznaniu_3.jpg
a2005_11_17_koncert_Riverisde_w_Poznaniu_4.jpg
a2005_11_17_koncert_Riverisde_w_Poznaniu_5.jpg
a2005_11_17_koncert_Riverisde_w_Poznaniu_6.jpg
b2006_01_22_wizyta_Panstwa_Rakowskich.jpg
b2006_01_22_wizyta_u_babci_Ali.jpg

it still gives me the wrong order.

Can you help me fixing it?
-------------------------------------------------------------------------------

function zczytaj_zdjecia_z_folderu($sciezka)
{

error_reporting(0);
$handle=opendir($sciezka);

if ($handle=="")
{
echo "<br>";
echo "<p align=\"center\">Brak plikow na dysku <b>".$sciezka."</b></p>";
$ilosc_zdjec_w_folderze=-1;
}
else
{
$licznik=0;
while (false !== ($file = readdir($handle)))
{
if ($file <"." && $file <"..")
{
$licznik++;
if ( (substr(strtoupper($file),strlen($file)-4,4)==".GIF") ||
(substr(strtoupper($file),strlen($file)-4,4)==".JPG") ||
(substr(strtoupper($file),strlen($file)-4,4)==".JPEG") ||
(substr(strtoupper($file),strlen($file)-4,4)==".BMP") ||
(substr(strtoupper($file),strlen($file)-4,4)==".PNG")
)


list($width, $height, $type, $attr) = getimagesize($sciezka.$file);

$item=$width/160;
$width_new=160;
$height_new=intval($height/$item);

if ($height_new>100)
{
$item=$height/100;
$height_new=100;
$width_new=intval($width/$item);
}


//najlepiej dobrany rozmiar to height=100; width=160;
{
?>
<a href="#" OnClick="funkcja('<?=$sciezka.$file; ?>');"><img
alt="<?=$file; ?>" src="<?=$sciezka."/".$file; ?>" border="1"
height="<?=$height_new; ?>" width="<?=$width_new; ?>"></a>
<?
if ($licznik==5) { echo "<br><br>"; $licznik=0;}
error_reporting(1);
// break;
}
}
else
{

}
}
}
error_reporting(1);

return (0);
}


----------------------------------------------------------------------------------------




Tim Van Wassenhove
Guest
 
Posts: n/a
#2: Apr 15 '07

re: order by file name readdir()


MZ schreef:
Quote:
Unfortunately images are not sorted by filename and I don`t know why.
From: http://www.php.net/readdir
Returns the filename of the next file from the directory. The filenames
are returned in the order in which they are stored by the filesystem.

So you still have to add code that sorts the filenames by name..

eg:

$filenames = array();
while ($filename = readdir($dir)) {
$filenames[] = $filename;
}

sort($filenames);

foreach($filename in $filenames) {
echo $filename . '<br/>';
}

--
Tim Van Wassenhove <url:http://www.timvw.be/>
ZeldorBlat
Guest
 
Posts: n/a
#3: Apr 15 '07

re: order by file name readdir()


On Apr 15, 8:27 am, Tim Van Wassenhove <t...@users.sourceforge.net>
wrote:
Quote:
MZ schreef:
>
Quote:
Unfortunately images are not sorted by filename and I don`t know why.
>
From:http://www.php.net/readdir
Returns the filename of the next file from the directory. The filenames
are returned in the order in which they are stored by the filesystem.
>
So you still have to add code that sorts the filenames by name..
>
eg:
>
$filenames = array();
while ($filename = readdir($dir)) {
$filenames[] = $filename;
>
}
>
sort($filenames);
>
foreach($filename in $filenames) {
echo $filename . '<br/>';
>
}
>
--
Tim Van Wassenhove <url:http://www.timvw.be/>
Alternatively you can use scandir() and specify the sorting_order
parameter:

<http://www.php.net/scandir>

MZ
Guest
 
Posts: n/a
#4: Apr 15 '07

re: order by file name readdir()



Uzytkownik "Tim Van Wassenhove" <timvw@users.sourceforge.netnapisal w
wiadomosci news:LqOdnYK4zsK_h7_bnZ2dnUVZ8sSrnZ2d@scarlet.biz. ..
Quote:
MZ schreef:
Quote:
>Unfortunately images are not sorted by filename and I don`t know why.
>
From: http://www.php.net/readdir
Returns the filename of the next file from the directory. The filenames
are returned in the order in which they are stored by the filesystem.
>
So you still have to add code that sorts the filenames by name..
>
eg:
>
$filenames = array();
while ($filename = readdir($dir)) {
$filenames[] = $filename;
}
>
sort($filenames);
>
foreach($filename in $filenames) {
echo $filename . '<br/>';
}

Thank you very much for help..

There is little error in the code. It should be:

foreach($filenames in $file) {
echo $file . '<br/>';
}


Cheers, thanx, hugs
Marcin


MZ
Guest
 
Posts: n/a
#5: Apr 15 '07

re: order by file name readdir()



Użytkownik "MZ" <marcinzmyslowski@poczta.onet.plnapisał w wiadomości
news:evtrkn$618$1@news.onet.pl...
Quote:
>
Uzytkownik "Tim Van Wassenhove" <timvw@users.sourceforge.netnapisal w
wiadomosci news:LqOdnYK4zsK_h7_bnZ2dnUVZ8sSrnZ2d@scarlet.biz. ..
Quote:
>MZ schreef:
Quote:
>>Unfortunately images are not sorted by filename and I don`t know why.
>>
>From: http://www.php.net/readdir
>Returns the filename of the next file from the directory. The filenames
>are returned in the order in which they are stored by the filesystem.
>>
>So you still have to add code that sorts the filenames by name..
>>
>eg:
>>
>$filenames = array();
>while ($filename = readdir($dir)) {
> $filenames[] = $filename;
>}
>>
>sort($filenames);
>>
>foreach($filename in $filenames) {
> echo $filename . '<br/>';
>}
>
>
Thank you very much for help..
>
There is little error in the code. It should be:
>
foreach($filenames in $file) {
echo $file . '<br/>';
}
>
>
Cheers, thanx, hugs
Marcin
>
Sorry it should be::

foreach($filenames as $file)
{
echo $file . '<br/>';
}


Closed Thread