By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

order by file name readdir()

P: n/a
MZ
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);
}
----------------------------------------------------------------------------------------

Apr 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
MZ schreef:
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/>
Apr 15 '07 #2

P: n/a
On Apr 15, 8:27 am, Tim Van Wassenhove <t...@users.sourceforge.net>
wrote:
MZ schreef:
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>

Apr 15 '07 #3

P: n/a
MZ

Uzytkownik "Tim Van Wassenhove" <ti***@users.sourceforge.netnapisal w
wiadomosci news:Lq******************************@scarlet.biz. ..
MZ schreef:
>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
Apr 15 '07 #4

P: n/a
MZ

Użytkownik "MZ" <ma**************@poczta.onet.plnapisał w wiadomo¶ci
news:ev**********@news.onet.pl...
>
Uzytkownik "Tim Van Wassenhove" <ti***@users.sourceforge.netnapisal w
wiadomosci news:Lq******************************@scarlet.biz. ..
>MZ schreef:
>>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/>';
}
Apr 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.