php : ONLY a few scripts to list files in a folder | Flavio's blog
 

php : ONLY a few scripts to list files in a folder

in webdesign, Web 2.0 & SEO | | 1 Comment » | Last modified:

Sometimes, for the most varied reasons, you want to list only a few files in a folder, and it can not be, not known, or you do not want to use the htaccess file..
Is possible to use a script (for example, I used here to show ONLY the files that begin with the letters “amsn”) in php, that, suitably configured by setting the string $list identifying which are the initials of the file to be displayed, do this without any particular problems.

Here is the code:

$dir_name = "/PATH/TO;
$list = "amsn";
$dir = opendir($dir_name);
$files = array();
while($file_name = readdir($you)) {
	if ((strpos($file_name, $list) !== False) && (strpos($file_name, $list) === 0)) {
		if(($file_name != ".") && ($file_name != ".."))	{
			$files[] = $ File_name;
		}
	}
}
closedir($you);
sort($files);
reset($files);
foreach($files as $ file)
{
echo "<LI>$File</LI>";
}

Obviously in $ dir_name must show the folder that contains the files you want to list, with absolute path.

E'quasi goes without saying that this script allows you to do something vaguely similar to using. Htaccess even on a Windows hosting.

Cute and useful, not?

 

1 Comment »

Please accept third-party cookies to be able to comment on the post! The CHANGE COOKIE CHOICES button is located in the footer of the site. / In order to comment this post, please accept the third party cookies! The button CAMBIA LE SCELTE DEI COOKIE is in the footer of the website.