Tags
PHP
Asked 8 years ago
6 Oct 2016
Views 1236
sarah

sarah posted

how to count total number of directories and files in given directory in php ?

Hi again . i am making file tree with jquery plugin for that i m making the php server side coding to load files and directory for particular given directory but i want to print first schema of directory like how much files and directories in there in this given directory ?
is this need any system call like "ls" in linux or "dir" in window by system function or we can do it any other way ?
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

count_Files_Folders is function here give you array of total number files and folders , i given big name for understanding . you can put shorten name instead .


function count_Files_Folders( $dir )
	{
		$total_file = 0;
		$total_dir = 0;

		if (is_dir($dir)) {
			$d = dir($dir);

			while (false !== ($entry = $d->read())) {
				if (substr($entry, 0, 1) != '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) ) {
					$total_file++;
				}
				if (substr($entry, 0, 1) != '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) {
					$total_dir++;
				}
			}

			$d->close();
		}

		return array ( $total_file, $total_dir );
	}


EnjoY coDDing
Post Answer