How to browse all files in folders and subfolders using PHP script

The following script allows you to browse files in folders and subfolders. The script uses a recursive function which displays files on the browser until all files have been read.

The script has been tested on Linux and it is working fine there however it may need some changes related to file path to work in Windows.

function scan_dir($dir) {
	$dirTemp = dir($dir);
	while (false !== ($entryTemp = $dirTemp->read())) {	
		if ($entryTemp != '.' && $entryTemp != '..') {
			// If it is a subfolder	
			if ($dir != '.') {
			 	if (is_dir($dir ."/". $entryTemp)) {
					scan_dir($dir ."/". $entryTemp);
				}
				else {	
					echo $dir ."/". $entryTemp . "
"; } } // If it is a root folder else { if (is_dir($entryTemp)) { scan_dir($entryTemp); } else { echo $entryTemp . "
"; } } } } return 1; } $a = scan_dir(".");
Please follow and like us:
error

Be the first to comment

Leave a Reply

Your email address will not be published.