On Feb 6, 2015, at 1:09 AM, Robert Stoll wrote:
Hey Jeffry,
-----Ursprüngliche Nachricht-----
Von: Jeffry Killen [mailto:jekillen@xxxxxxxxxxx]
Gesendet: Freitag, 6. Februar 2015 07:40
An: PHP General
Betreff: Help with recursive function
Hello again;
I am writing a recursive file system reading method (just for the
challenge, i am re inventing a wheel)
and I am only getting to the third level below the initial dir when
I know there is five levels below.
I don't see why:
private function getDirList($_dir)
{
$_out = array();
$_valid = '';
$_DR = opendir($_dir);
while($_x = readdir($_DR))
{
switch($_x)
{
case '.':
case '..':
break;
default:
if(is_dir($_x))
{
$_valid = self::screen($_dir.'/'.$_x,
'dir');
if($_valid['error'])
{
self::$_readError[count(self::
$_readError)] = $_valid['error'];
continue;
}
else if($_valid['pass'])
{
self::$_dirsToRead[$_dir.'/'.$_x] = true;
self::getDirList($_dir.'/'.$_x); ///
<<<< this should accumulate directories, shouldn't
it?
}
}
else if(is_file)
{
continue;
}
break;
}
}
closedir($_DR);
}
thank you for time and attention
JK
--
PHP General Mailing List (http://www.php.net/) To unsubscribe,
visit: http://www.php.net/unsub.php
I do not see that you are actually doing something with the
directories. You never store them in $_out nor are you
returning $_out. Might this be the error? Probably you omitted some
code, if so, then please provide the whole code (you
could past it to 3v4l.org and provide the link)
One other thing I spotted is the else if branch:
else if(is_file)
should be
else if(is_file($_x))
Cheers,
Robert
Thank you for pointing out the "else if(is_file)" error
Also, I have been in such a habit of adding $_out = array()
that I did that here but it is not being used in this method.
The current status is that I am getting a valid list of directories
and no self::$_readErrors. It is just not going deep enough.
This line
self::$_dirsToRead[$_dir.'/'.$_x] = true;
collects the directories as the code finds them.
It is a static member variable.
There is another public method that calls this method.
public function run($_do, $_params)// $_params should be an array
{
$_out = array();
// do gettype on $_params
$_stat = '';
$_argType = gettype($_params);
$_execType = gettype($_do);
if($_argType != 'array')
{
$_stat = "parameters argument must be and array:
but is: ".$_argType;
}
if($_execType != 'string')
{
if($_stat)
{
$_stat .= "\nexecution argument must be a
string, but is ".$_execType;
}
else
{
$_stat = "execution argument must be a string,
but is ".$_execType;
}
}
if($_stat)
{
$_out['error'] = self::$_className."->run error:
Bad input argument type: ".$_stat;
return $_out;
}
switch($_do)
{
case 'dirList':
self::getDirList($_params['dir']);
$_out['doing'] = $_do;
$_out['dirs'] = self::$_dirsToRead;
$_out['error'] = self::$_readError;
break;
//// etc... The rest of the cases are just for testing purposes at
the moment.
}
return $_out;
}
As you can see in the case label, self::$_dirsToRead is returned as
$_out['dirs'];
This line in the code: $_valid = self::screen($_dir.'/'.$_x, 'dir');
checks to see if the directory in question is readable and executable.
If not it is added to self::$_readError
and skipped.
The class def contains:
// etc...
private static $_readError = array();
private static $_dirsToRead = array();
private static $_dirsRead = array();
private static $_className = '';
public function __construct()
{
self::$_homePath = getcwd();
self::$_className = get_class($this);
}
// etc...
I hope this is enough,
thanks for further time and attention
JK
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php