Array_walk and scanfiles

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!

Moderator: General Moderators

Post Reply
Tsail
Forum Newbie
Posts: 3
Joined: Sun Jan 24, 2010 3:51 pm

Array_walk and scanfiles

Post by Tsail »

Hi,
I am trying to create a serries of arrays, where there is an array for every folder listing the files in that folder, so far I have come up with this but it isn't working and keeps returning this error:
Warning: scandir(0.) [function.scandir]: failed to open dir: No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir() [function.scandir]: (errno 0): No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir(1..) [function.scandir]: failed to open dir: No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir() [function.scandir]: (errno 0): No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir(201) [function.scandir]: failed to open dir: No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir() [function.scandir]: (errno 0): No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir(302) [function.scandir]: failed to open dir: No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43

Warning: scandir() [function.scandir]: (errno 0): No error in \\NAS40ENT\domains\b\braindeadproductions.eu\user\htdocs\php_pages\galleries\2009.php on line 43
I think this is because of the order php is doing things but i have no idea how to fix it :(

Any help will be greatly appreciated.

Thanks

Tsail


P.S.

This is the code I'm using:

Code: Select all

<?php
$dir    = '../../Gallery/2009/';
$file = scandir($dir);
 
function scanfiles($value, $dir    = '../../Gallery/2009/')
{scandir($dir . $value);
}
 
array_walk($file,"scanfiles");
 
?>
Thanks
User avatar
a.heresey
Forum Commoner
Posts: 59
Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US

Re: Array_walk and scanfiles

Post by a.heresey »

I think what is happening is that you are passing files like they were directories. Use is_dir() to check whether each value is a directory before you call scandir() again.

Code: Select all

 
<?php
$dir    = '../../Gallery/2009/';
$file = scandir($dir);//this is an array
 
function scanfiles(&$value, $dir    = '../../Gallery/2009/')
{
if(is_dir($value)){//check if directory
$value= scandir($value);//scan the directory nest array
}
 
}
 
array_walk($file,"scanfiles");
 
?>
 
 
Tsail
Forum Newbie
Posts: 3
Joined: Sun Jan 24, 2010 3:51 pm

Re: Array_walk and scanfiles

Post by Tsail »

Thanks :) is not showing any errors so I think its working, when using the echo would i refer to the array as $value ?

Thanks again
User avatar
a.heresey
Forum Commoner
Posts: 59
Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US

Re: Array_walk and scanfiles

Post by a.heresey »

$file should now be a multi-dimensional array that you would then run the echo on. Try array_walk_recursive to get all of the file names to echo (directory names will not print)

Code: Select all

 
function echo_array($v){
echo $v;
}
array_walk_recursive($file, 'echo_array');
 
Post Reply