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
Actually, I think I understand what you mean by code defensively:
instead of this:
@$_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'];
}
it should be this:
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'];
}
$_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'];
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php