Re: Opening and reading director produces random order

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Dec 9, 2014, at 12:13 PM, Ashley Sheridan wrote:



On 9 December 2014 20:05:42 GMT+00:00, Jeffry Killen <jekillen@xxxxxxxxxxx > 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 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





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux