Jump to content

Checking directory for specific file extension


skulky

Recommended Posts

Hi there,

I need to check for a specific extension (eg: ".txt") inside a directory.

If the files are there they get listed in a table.

If they are not then the user gets a message saying eg: "No such files here".

 

Here is what I have so far...I can't seem to display the "No such files here" message without repeating several times with the loop.

 

	$fileList = scandir($dir);
	rsort($fileList); //to see the latest file on top
	function extCheck($fileArray, $filename, $extAllow){
		if(substr($fileArray[$filename],-3,3)==$extAllow){
			$GLOBALS['list'] = $list;
			$list = "<a href=\"".$urlClientString.$fileArray[$filename]."\" >".$fileArray[$filename]."</a>".
			" - added on ".
			 date ("F d Y", filemtime($fileArray[$filename])).
			"<br/>";
			echo $list;
		}
		else
		{
			unset($fileArray[$filename]);//trying to delete all files, except the ones with ".txt" extension
			//The array seems to refill itself each time though
		}
	}
	for($fNum=0,$fCount = count($fileList); $fNum<$fCount; $fNum++){
		extCheck($fileList, $fNum, "txt");
	}	
	if(!$list){//Checking if $list contains any string...
		echo "There are no reports in your database.";
	}		

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.