FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » sorting readdir output?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
sorting readdir output? [message #183997] Sun, 01 December 2013 16:39 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
<?php

if ($handle = opendir('../audio/1960/')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";

/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n<br>";
}

closedir($handle);
}
?>

This gives the output in an unsorted list.
How can I make it so the array is sorted?

http://us1.php.net/readdir
Re: sorting readdir output? [message #183998 is a reply to message #183997] Sun, 01 December 2013 16:56 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 12/01/2013 11:39 AM, richard wrote:
> <?php
>
> if ($handle = opendir('../audio/1960/')) {
> echo "Directory handle: $handle\n";
> echo "Entries:\n";
>
> /* This is the correct way to loop over the directory. */
> while (false !== ($entry = readdir($handle))) {
> echo "$entry\n<br>";
> }
>
> closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
>
> http://us1.php.net/readdir
>

I would think it's be obvious that you need to *SORT* the $entry
*ARRAY* before you output anything.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: sorting readdir output? [message #183999 is a reply to message #183998] Sun, 01 December 2013 17:13 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sun, 01 Dec 2013 11:56:04 -0500, Norman Peelman wrote:

> On 12/01/2013 11:39 AM, richard wrote:
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>> echo "Directory handle: $handle\n";
>> echo "Entries:\n";
>>
>> /* This is the correct way to loop over the directory. */
>> while (false !== ($entry = readdir($handle))) {
>> echo "$entry\n<br>";
>> }
>>
>> closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
>>
>> http://us1.php.net/readdir
>>
>
> I would think it's be obvious that you need to *SORT* the $entry
> *ARRAY* before you output anything.

http://s2.quickmeme.com/img/e2/e2f5aa6bfb9d5754ab2678075f95572c502ae22456f1 25bf4c7ac4aaf055af85.jpg

http://www.quickmeme.com/img/ca/ca04c469a453cde96a177e91997888d9ff3a0157427 26d1234651dbd7c5ae780.jpg
Re: sorting readdir output? [message #184000 is a reply to message #183997] Sun, 01 December 2013 17:32 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 01-12-2013 17:39, richard wrote:
> <?php
>
> if ($handle = opendir('../audio/1960/')) {
> echo "Directory handle: $handle\n";
> echo "Entries:\n";
>
> /* This is the correct way to loop over the directory. */
> while (false !== ($entry = readdir($handle))) {
> echo "$entry\n<br>";
> }
>
> closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?


which array?
http://us2.php.net/manual/en/language.types.array.php

but if you DO have an array, dan sorting is easy:
http://us2.php.net/manual/en/function.sort.php

>
> http://us1.php.net/readdir
>

yep, it's all in the manual....
Re: sorting readdir output? [message #184001 is a reply to message #184000] Sun, 01 December 2013 17:35 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 01-12-2013 18:32, Luuk wrote:
> On 01-12-2013 17:39, richard wrote:
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>> echo "Directory handle: $handle\n";
>> echo "Entries:\n";
>>
>> /* This is the correct way to loop over the directory. */
>> while (false !== ($entry = readdir($handle))) {
>> echo "$entry\n<br>";
>> }
>>
>> closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
>
>
> which array?
> http://us2.php.net/manual/en/language.types.array.php
>
> but if you DO have an array, dan sorting is easy:
> http://us2.php.net/manual/en/function.sort.php
>
>>
>> http://us1.php.net/readdir
>>
>
> yep, it's all in the manual....


and there's even an extended manual:
http://lmgtfy.com/?q=how+to+get+a+sorted+list+of+files+in+php&l=1
Re: sorting readdir output? [message #184002 is a reply to message #183997] Sun, 01 December 2013 17:59 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
On Sunday 01 December 2013 11:39, in comp.lang.php, "richard"
<noreply(at)example(dot)com> wrote:

> <?php
>
> if ($handle = opendir('../audio/1960/')) {
> echo "Directory handle: $handle\n";
> echo "Entries:\n";
>
> /* This is the correct way to loop over the directory. */
> while (false !== ($entry = readdir($handle))) {
> echo "$entry\n<br>";
> }
>
> closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?

Sort it, of course.

As you read the directory, put each entry into the same array.
Once you've hit the end, sort the array.



--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
Re: sorting readdir output? [message #184003 is a reply to message #184002] Sun, 01 December 2013 18:10 Go to previous messageGo to next message
James Harris is currently offline  James Harris
Messages: 11
Registered: November 2013
Karma: 0
Junior Member
"Lew Pitcher" <lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote in message
news:vOKmu(dot)376035$jg4(dot)217970(at)fx05(dot)iad...

....

>> if ($handle = opendir('../audio/1960/')) {

....

>> while (false !== ($entry = readdir($handle))) {
>> echo "$entry\n<br>";
>> }

....

>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?

....

> As you read the directory, put each entry into the same array.
> Once you've hit the end, sort the array.

How about using scandir?

$ents = scandir($document_root . $dir);
natcasesort($ents);

James
Re: sorting readdir output? [message #184004 is a reply to message #184000] Sun, 01 December 2013 18:10 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sun, 01 Dec 2013 18:32:59 +0100, Luuk wrote:

> On 01-12-2013 17:39, richard wrote:
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>> echo "Directory handle: $handle\n";
>> echo "Entries:\n";
>>
>> /* This is the correct way to loop over the directory. */
>> while (false !== ($entry = readdir($handle))) {
>> echo "$entry\n<br>";
>> }
>>
>> closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
>
>
> which array?
> http://us2.php.net/manual/en/language.types.array.php
>
> but if you DO have an array, dan sorting is easy:
> http://us2.php.net/manual/en/function.sort.php
>
>>
>> http://us1.php.net/readdir
>>
>
> yep, it's all in the manual....

The array is created from $handle I believe.
Re: sorting readdir output? [message #184005 is a reply to message #184004] Sun, 01 December 2013 18:19 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
On Sunday 01 December 2013 13:10, in comp.lang.php, "richard"
<noreply(at)example(dot)com> wrote:

> On Sun, 01 Dec 2013 18:32:59 +0100, Luuk wrote:
>
>> On 01-12-2013 17:39, richard wrote:
>>> <?php
>>>
>>> if ($handle = opendir('../audio/1960/')) {
>>> echo "Directory handle: $handle\n";
>>> echo "Entries:\n";
>>>
>>> /* This is the correct way to loop over the directory. */
>>> while (false !== ($entry = readdir($handle))) {
>>> echo "$entry\n<br>";
>>> }
>>>
>>> closedir($handle);
>>> }
>>> ?>
>>>
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
>>
>>
>> which array?
>> http://us2.php.net/manual/en/language.types.array.php
>>
>> but if you DO have an array, dan sorting is easy:
>> http://us2.php.net/manual/en/function.sort.php
>>
>>>
>>> http://us1.php.net/readdir
>>>
>>
>> yep, it's all in the manual....
>
> The array is created from $handle I believe.

You believe wrong.

opendir() returns a "handle", which is a "resource", and a "resource" is "a
special variable, holding a reference to an external resource."

The handle returned by opendir(), that is used in readdir() and closedir()
is NOT an array or list of directory entries.

--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
Re: sorting readdir output? [message #184006 is a reply to message #184003] Sun, 01 December 2013 18:19 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sun, 1 Dec 2013 18:10:09 -0000, James Harris wrote:

> "Lew Pitcher" <lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote in message
> news:vOKmu(dot)376035$jg4(dot)217970(at)fx05(dot)iad...
>
> ...
>
>>> if ($handle = opendir('../audio/1960/')) {
>
> ...
>
>>> while (false !== ($entry = readdir($handle))) {
>>> echo "$entry\n<br>";
>>> }
>
> ...
>
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
>
> ...
>
>> As you read the directory, put each entry into the same array.
>> Once you've hit the end, sort the array.
>
> How about using scandir?
>
> $ents = scandir($document_root . $dir);
> natcasesort($ents);
>
> James

This one does what I want nicely.
thanks.

<?php
$dir = "/tmp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?>
Re: sorting readdir output? [message #184007 is a reply to message #183997] Sun, 01 December 2013 23:41 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 01 Dec 2013 11:39:20 -0500, richard wrote:

> <?php
>
> if ($handle = opendir('../audio/1960/')) {
> echo "Directory handle: $handle\n";
> echo "Entries:\n";
>
> /* This is the correct way to loop over the directory. */
> while (false !== ($entry = readdir($handle))) {
> echo "$entry\n<br>";

Make sure here that you use the form of break tag that matches your
doctype.

> }
>
> closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
>
> http://us1.php.net/readdir

What array? At the moment you're working with a directory handle, from
which you're reading one filename at a time. They come in whatever order
the underlying os code delivers them.

If you want them sorted, read them in to an array and sort it, and then
output the array in order, but at the moment, in your code, there is no
array.

Without testing, the following might work:

<?php

$files = array();

if ( $handle = opendir ( '../audio/1960/' ) ) while ( false !== ( $entry
= readdir( $handle ) ) ) $files[] = $entry;

sort( $files );

foreach ( $files as $file ) echo "{$file}<br>\n";

?>

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: sorting readdir output? [message #184020 is a reply to message #183997] Mon, 02 December 2013 18:54 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply(at)example(dot)com>
wrote:

Isn't "false !==" unnecessary?


> <?php
>
> if ($handle = opendir('../audio/1960/')) {
> echo "Directory handle: $handle\n";
> echo "Entries:\n";
>
> /* This is the correct way to loop over the directory. */
> while (false !== ($entry = readdir($handle))) {
> echo "$entry\n<br>";
> }
>
> closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
>
> http://us1.php.net/readdir
Re: sorting readdir output? [message #184021 is a reply to message #184020] Mon, 02 December 2013 20:24 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 02 Dec 2013 10:54:49 -0800, Richard Yates wrote:

> On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply(at)example(dot)com>
> wrote:
>
> Isn't "false !==" unnecessary?
>
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>> echo "Directory handle: $handle\n";
>> echo "Entries:\n";
>>
>> /* This is the correct way to loop over the directory. */
>> while (false !== ($entry = readdir($handle))) {
>> echo "$entry\n<br>";
>> }
>>
>> closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
>>
>> http://us1.php.net/readdir

1. Please post replies *BELOW* the points they address, this is usenet,
we've been reading from down the page for years now and it's the way we'd
like to continue doing it.

2. Your question could be easily answered by reading the relevant manual
entry for the function concerned. Possibly much faster than by posting to
usenet and waiting for a response. Perhaps you should try that next time?

3. Said manual entry ( http://www.php.net/manual/en/function.readdir.php
) contains:

"Return Values

Returns the entry name on success or FALSE on failure.

Warning

This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE. Please read the section on Booleans for
more information. Use the === operator for testing the return value of
this function."

See: http://www.php.net/manual/en/language.types.boolean.php

Which includes:

"Converting to boolean

To explicitly convert a value to boolean, use the (bool) or (boolean)
casts. However, in most cases the cast is unnecessary, since a value will
be automatically converted if an operator, function or control structure
requires a boolean argument.

See also Type Juggling.

When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0" <----------------------****
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource)."

So yes, "false !==" is needed, because you can have a valid result which
would evaluate to false (such as the valid file name "0").

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: sorting readdir output? [message #184022 is a reply to message #184021] Mon, 02 December 2013 21:02 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Mon, 2 Dec 2013 20:24:54 +0000 (UTC), Denis McMahon
<denismfmcmahon(at)gmail(dot)com> wrote:

> On Mon, 02 Dec 2013 10:54:49 -0800, Richard Yates wrote:
>
>> On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply(at)example(dot)com>
>> wrote:
>>
>> Isn't "false !==" unnecessary?
>>
>>> <?php
>>>
>>> if ($handle = opendir('../audio/1960/')) {
>>> echo "Directory handle: $handle\n";
>>> echo "Entries:\n";
>>>
>>> /* This is the correct way to loop over the directory. */
>>> while (false !== ($entry = readdir($handle))) {
>>> echo "$entry\n<br>";
>>> }
>>>
>>> closedir($handle);
>>> }
>>> ?>
>>>
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
>>>
>>> http://us1.php.net/readdir
>
> 1. Please post replies *BELOW* the points they address, this is usenet,
> we've been reading from down the page for years now and it's the way we'd
> like to continue doing it.
>
> 2. Your question could be easily answered by reading the relevant manual
> entry for the function concerned. Possibly much faster than by posting to
> usenet and waiting for a response. Perhaps you should try that next time?
>
> 3. Said manual entry ( http://www.php.net/manual/en/function.readdir.php
> ) contains:
>
> "Return Values
>
> Returns the entry name on success or FALSE on failure.
>
> Warning
>
> This function may return Boolean FALSE, but may also return a non-Boolean
> value which evaluates to FALSE. Please read the section on Booleans for
> more information. Use the === operator for testing the return value of
> this function."
>
> See: http://www.php.net/manual/en/language.types.boolean.php
>
> Which includes:
>
> "Converting to boolean
>
> To explicitly convert a value to boolean, use the (bool) or (boolean)
> casts. However, in most cases the cast is unnecessary, since a value will
> be automatically converted if an operator, function or control structure
> requires a boolean argument.
>
> See also Type Juggling.
>
> When converting to boolean, the following values are considered FALSE:
>
> the boolean FALSE itself
> the integer 0 (zero)
> the float 0.0 (zero)
> the empty string, and the string "0" <----------------------****
> an array with zero elements
> an object with zero member variables (PHP 4 only)
> the special type NULL (including unset variables)
> SimpleXML objects created from empty tags
>
> Every other value is considered TRUE (including any resource)."
>
> So yes, "false !==" is needed, because you can have a valid result which
> would evaluate to false (such as the valid file name "0").

(Sorry for the previous top post)

I understand the warning as it applies to many functions. Perhaps I
should have asked why there would ever be a valid result of a
directory read that evaluates to false. In practice, reading his own
directory, would he ever actually encounter a file named "0" or
"false"?
Re: sorting readdir output? [message #184023 is a reply to message #184022] Mon, 02 December 2013 21:33 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Richard Yates wrote:

> I understand the warning as it applies to many functions. Perhaps I
> should have asked why there would ever be a valid result of a
> directory read that evaluates to false. In practice, reading his own
> directory, would he ever actually encounter a file named "0" or
> "false"?

Using the explicit check for !== FALSE seems reasonable anyway. A file
named "0" might exists, and maybe there are some strange filenames that
are returned as empty string (not sure, if that could happen, though).
The filename "false", however, == TRUE.

OTOH, implicitely checking for == FALSE, would catch an error where
opendir() returned FALSE (for instance, because the directory does not
exist). In this case the OP's code would call readdir() with FALSE as
argument, what would cause readdir() to return NULL.

A better solution would check for the return value of opendir() in the
first place.

--
Christoph M. Becker
Re: sorting readdir output? [message #184024 is a reply to message #184021] Mon, 02 December 2013 22:20 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <l7iqam$7ms$4(at)dont-email(dot)me>, Denis McMahon
<denismfmcmahon(at)gmail(dot)com> wrote:

> 1. Please post replies *BELOW* the points they address, this is usenet,
> we've been reading from down the page for years now and it's the way we'd
> like to continue doing it.

Well you might have been, personally I prefer replies to be
interspersed so as to be next to the salient point.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: sorting readdir output? [message #184025 is a reply to message #184024] Mon, 02 December 2013 23:54 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 02 Dec 2013 22:20:06 +0000, Tim Streater wrote:

> In article <l7iqam$7ms$4(at)dont-email(dot)me>, Denis McMahon
> <denismfmcmahon(at)gmail(dot)com> wrote:
>
>> 1. Please post replies *BELOW* the points they address, this is usenet,
>> we've been reading from down the page for years now and it's the way
>> we'd like to continue doing it.
>
> Well you might have been, personally I prefer replies to be interspersed
> so as to be next to the salient point.

Well, yes, but still underneath, so that the chain of comments giving
context is reading top to bottom, not bottom to top.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: dropdown menus
Next Topic: Re: SEO SERVICES
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Dec 04 09:08:38 GMT 2024

Total time taken to generate the page: 0.03517 seconds