Recursively reading files based on search option in PHP 4 and 5

Sunday, July 17, 2011 6:36
Posted in category PHP

Recursively reading files based on search option in PHP 4 and 5. It is a small script written in equivalent to Directory.GetFiles method in C#.

Small script to read xml files recursively from a specified directory. Return the results in array format. Same code is written in little different format to make it to work in PHP 4 and 5 versions.

PHP 4

<?php
	$dir_path = '.';
	$files = get_files($dir_path, '.xml', 'r');
	print_r($files); // return all xml files in array format.

	function get_files($dir_path = '.', $searchPattern = '', $searchOption = '') {

		$files = array();

		if (is_dir($dir_path)) {
			if ($handle = opendir($dir_path)) {
				$l_searchPattern = (!empty($searchPattern) && substr($searchPattern,0,1) == '*') ? '.' . substr($searchPattern,0,1) . preg_quote(substr($searchPattern,1)) : $searchPattern;
				while (($file = readdir($handle)) !== false) {
					if ($file == "." || $file == ".." ) continue;

					$complete_filepath = $dir_path .'/'. $file;

					if (is_dir($complete_filepath) && ($searchOption == 'recursive' || $searchOption == 'r')) {
						$files = array_merge($files, get_files($complete_filepath, $searchPattern, $searchOption));
						continue;
					}

					if (!is_dir($complete_filepath) && empty($l_searchPattern) || preg_match("/$l_searchPattern$/i", $file)) {
						array_push($files, $complete_filepath);
					}
				}
				closedir($handle);
			}
		}

		return $files;
	}
?>

PHP 5

<?php

	$dir_path = '.';

	$files = get_files($dir_path, '.xml', 'r');

	print_r($files);

	function get_files($dir_path = '.', $searchPattern = '', $searchOption = '') {

		$files = array();

		if (is_dir($dir_path)) {
			$scan_files = scandir($dir_path);

			$l_searchPattern = (!empty($searchPattern) && substr($searchPattern,0,1) == '*') ? '.' . substr($searchPattern,0,1) . preg_quote(substr($searchPattern,1)) : $searchPattern;
			foreach($scan_files as $file) {
				if ($file == "." || $file == ".." ) continue;

				$complete_filepath = $dir_path .'/'. $file;

				if (is_dir($complete_filepath) && ($searchOption == 'recursive' || $searchOption == 'r')) {
					$files = array_merge($files, get_files($complete_filepath, $searchPattern, $searchOption));
					continue;
				}

				if (!is_dir($complete_filepath) && empty($l_searchPattern) || preg_match("/$l_searchPattern$/i", $file)) {
					array_push($files, $complete_filepath);
				}
			}
		}

		return $files;
	}

?>

I have used scandir function in PHP 5 version, which is easiest way to read directories and files from specific directory. To learn more about scandir function – http://php.net/manual/en/function.scandir.php.

In get_files function, there are three parameters
1. $dir_path – The path you want to read files
2. $searchPattern – By default it is empty, If you don’t pass any value, then it will return all files. If you want only xml files then pass “.xml” or “*.xml”
3. $searchOption – By default it is empty, If you pass empty it will return files from top directory only. To recursively get files based on searchPattern then pass “recursive” or “r”.

Thanks for reading :)

You can leave a response, or trackback from your own site.

2 Responses to “Recursively reading files based on search option in PHP 4 and 5”

  1. LP says:

    November 4th, 2011 at 2:06 am

    Is there any way to read the data in each .xml file and export to a csv or text for each listing?

  2. admin says:

    November 5th, 2011 at 7:25 pm

    $files = get_files($dir_path, ‘.xml’, ‘r’);

    Read the xml files by loop $files array, then based on xml format, you can convert into your CSV ot TXT format.

Leave a Reply

*