PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
public function findJpgs($dir)
{
if (!is_dir($dir))
{
trigger_error('Cannot find directory: ' . $dir, E_USER_ERROR);
}
$handle = opendir($dir);
while (false !== ($file = readdir($handle)))
{
if (is_dir($file))
{
$this->findJpgs($file);
} else
{
if (preg_match('/^.+?\.jp[e]?g$/i', $file))
{
echo $file . "\n";
}
}
}
}
Tried this on the command line and browser.. it causes CLI to error and exit, and causes apache to stop working.
I've dealt with recursive functions, and the above should work in a function (haven't tested). Is there anything special I have to do if it's in a method?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
My guess is $this->findJpgs($file) needs a relative path (ie. $dir . '/' . $file), but I've never had that problem with recursion functions.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Okay, wow. . and .. were causing an infinite (or at least very long) loop. So I got rid of those.
Turns out my next problem is a php bug. is_dir() won't support directories on windows given with a relative path. I need to use the absolute path, which will be a bit of a tough to figure out.
Unless there is a function to get the path of a given file?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
volka wrote:The function shouldn't call itself for . and .. in any given directory.
Are you saying that it shouldn't follow . and .. if my coding tells it to? Because before I added the check to skip those directories, it definitely followed them and not only sent me into a very long loop, but crashed the web server and cli.
As for the bug, this one seems to be closest to what I was looking at last night.
I'm probably just going to finish developing this script on my ubuntu box. Trying to complete it on windows is making me really hack it up by using if (!is_file()) instead of is_dir().
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.