Jump to content

recursive directory function


grasperstail

Recommended Posts

I can't seem to figure out why the $folders array and the $files array are being overwritten (starting from 0 key) when the function goes into recursion.

function photo_directory($start_dir){		$files = array();	$dir_array = array();	$folders = array();			if(is_dir($start_dir)){		$fh = opendir($start_dir);		while (false !== ($file = readdir($fh))){		if (strcmp($file, ".")==0 || strcmp($file, "..") == 0) continue;		$filepath = $start_dir . "/" . $file;		print_r($filepath);		echo "</br>";		if(is_dir($filepath)){						$folders[] = $filepath;			print_r($folders);			echo "</br>";			photo_directory($filepath);											}else{					$files[] = $filepath;			print_r($files);			echo "</br>";						}			}	closedir($fh);	}else{	$dir_array = false;	}	return $dir_array;	}$test = photo_directory($start_dir);

Link to comment
Share on other sites

Because you haven't declared them as global

function photo_directory($start_dir){		global $files;	$dir_array = array();	gloabl $folders;			if(is_dir($start_dir)){		$fh = opendir($start_dir);		while (false !== ($file = readdir($fh))){		if (strcmp($file, ".")==0 || strcmp($file, "..") == 0) continue;		$filepath = $start_dir . "/" . $file;		print_r($filepath);		echo "</br>";		if(is_dir($filepath)){						$folders[] = $filepath;			print_r($folders);			echo "</br>";			photo_directory($filepath);											}else{					$files[] = $filepath;			print_r($files);			echo "</br>";						}			}	closedir($fh);	}else{	$dir_array = false;	}	return $dir_array;	}$files = array();$folders = array();$test = photo_directory($start_dir);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...