Re: Opening and reading director produces random order
| From: | Aziz Saleh | Date: | Tue, 09 Dec 2014 20:28:11 +0000 |
| Subject: | Re: Opening and reading director produces random order | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324104@lists.php.net to get a copy of this message | ||
On Tue, Dec 9, 2014 at 3:23 PM, Jeffry Killen <jekillen@prodigy.net> wrote:
>
> On Dec 9, 2014, at 12:13 PM, Ashley Sheridan wrote:
>
>
>>
>> On 9 December 2014 20:05:42 GMT+00:00, Jeffry Killen <
>> jekillen@prodigy.net> wrote:
>>
>>> Hello;
>>>
>>> I have the following section of code that opens and reads a dir and
>>> assembles the contents
>>> into html table markup:
>>>
>>> The question is: when this is run on my remote hosting service, the
>>> list comes out in a random order, Why and what can I do
>>> programatically to correct the order?
>>> The remote system is running on a Linux platform, but that shouldn't
>>> make a difference. I seem to remember somewhere in the
>>> php manual that the sort order may be random and that there was a
>>> function for producing a natural sort(?)
>>> (while at home in local dev server: Apache running on Mac OSX, the
>>> list comes out in the order it
>>> appears in the directory)
>>>
>>> Thank you for time and attention
>>> JK
>>>
>>> the code:
>>>
>>> @$_dr = opendir($_dir);
>>> if(!$_dr)
>>> {
>>> $_stat = '';
>>> if(!is_dir($_dir))
>>> {
>>> $_stat = 'not found';
>>> }
>>> else if(!is_readable($_dir))
>>> {
>>> $_stat = 'not readable';
>>> }
>>> $_out['error'] = "_CONSOLE->mkList() error: ".
>>> $_dir." not opened for read: ".$_stat;
>>> return $_out['error'];
>>> }
>>> $_itr = 0;
>>> $_out['mrkUp'] =
>>> "<table><tr><td><span class=\"norm
>>> \">Owner</span></td><td><span
>>> class=\"norm\">type and permissions</
>>> span></td><td><span class=\"norm\">Octal
>>> span>value</span></td><td><span
>>> class=\"norm\">Name</span></td></tr>\n";
>>> while(false !== ($_x = readdir($_dr)))
>>> {
>>> $_owner = self::getOwner($_dir.$_x);
>>> $_octPerms = substr(sprintf('%o', fileperms($_dir.
>>> $_x)), -4);
>>> $_out['mrkUp'] .= "<tr><td><span
>>> class=\"norm\" id=
>>>
>>> \"o_".$_itr."\">".$_owner."</span></td><td><span
>>> class=\"norm\" id=
>>>
>>> \"p_".$_itr."\">".self::getWRXPerms($_dir.$_x)."</span></td><td><span
>>> class=\"norm\"
>>> id=\"r_".$_itr."\">".$_octPerms."</span></td><td><span
>>> class=\"norm\"
>>>
>>> id=\"n_".$_itr."\">".basename($_x)."</span></td></tr>\n";
>>> $_itr++;
>>> }
>>> closedir($_dr);
>>> $_out['mrkUp'] .= "</table>";
>>> return $_out;
>>>
>>
>> I believe the natural order of a directory listing on linux is by the
>> internal inode number. It should be easy though to put the listing results
>> into an array first and then sort that however you wish.
>>
>> Also, one thing you should always avoid is the use of @ to suppress
>> errors. If you think your code will cause an error, code defensively, or at
>> the least use exception handling.
>> Thanks,
>> Ash
>>
>
> Thank you for the info:
> As far as not using @ to surpress errors, I am also aware of using try and
> catch slowing the code execution down.
> I don't want a page with only an error message smeared over it and nothing
> else. So why is it a bad thing?
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Unlike Ashley, I think that the error suppressor has no place in PHP, not
sure why it is even still there. Personally, I think it is a very bad
coding practice.
What the error suppressor does is trying to hide the symptoms of a disease
as oppose to curing it. Try/Catch is one way to avoid it, but other ways
can be as easily as if() statement.