Freelancer's Playground! New knowledges every now and then

28Apr/092

PHP Simple File Browser

While browsing my google analytics logs, I stumble upon certain keyword search that looking for file browsers script. I don't have one here, so I created it, so that next time people searching for PHP file browser and landed on this blog, they will get what they want (hopefully).

Without further ado, here's the code, grab it and use it the way you like. No need to think about copyright. Just remember, if you do use it in your commercial product, please tell me. No, I will not ask for money, I just want to know what commercial product use my script.

<?php

define('ROOTDIR', dirname(__FILE__));//make sure no trailing slash in it.
define('ROOTURL', '');//make sure no trailing slash in it.
define('EXTMODE', 'deny');//accepted options: 'allow', 'deny'
//if EXTMODE=allow, only EXTLIST is allowed
//if EXTMODE=deny, extensions listed in EXTLIST is denied
//EXTLIST format: ext1|ext2|ext3|ext4
//   eg: php|zip|htaccess
define('EXTLIST', 'pdf|txt~|htaccess');

$subdir = isset($_GET['s']) ? $_GET['s'] : '';
//cleanup $subdir
$subdir = str_replace('..', '', $subdir);

$nowdir = ROOTDIR.(empty($subdir) ? '' : '/'.$subdir);
$nowurl = ROOTURL.(empty($subdir) ? '' : '/'.$subdir);

$dirs = array();
$files = array();
if (is_dir($nowdir)) {
    if ($dh = @opendir($nowdir)) {
        while (($file = readdir($dh)) !== false) {
            //echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
			if(!in_array($file, array('.', '..')))
			{
				$cfile = $nowdir.'/'.$file;
				$tmp = pathinfo($cfile);
				$file_extension = $tmp['extension'];
				unset($tmp);

				$tmp = trim(EXTLIST, "|" );
				$exts = explode("|", $tmp);
				unset($tmp);

				if(is_dir($cfile))
				{
					$dirs[] = $file;
				}
				else
				{
					if("allow" == EXTMODE)
					{
						if(in_array($file_extension, $exts))
						{
							$files[] = $file;
						}
					}
					else if("deny" == EXTMODE)
					{
						if(!in_array($file_extension, $exts))
						{
							$files[] = $file;
						}
					}
				}

			}
        }
        closedir($dh);
    }
	else
	{
		echo "ERROR:unable to open path!";
	}
	echo "<div class='header'>Directories</div>\n";
	echo "<ul>\n";
	foreach($dirs as $dir)
	{
		echo "\t<li><a href='?s=$subdir/$dir'>$dir/</a></li>\n";
	}
	echo "</ul>\n";

	echo "<div class='header'>Files</div>\n";
	echo "<ul>\n";
	foreach($files as $file)
	{
		echo "\t<li><a href='$nowurl/$file'>$file</a></li>\n";
	}
	echo "</ul>\n";

}
else
{
	echo "INVALID DIRECTORY!";
}

?>

  • http://beta.ferdianto.com/ ferdhie

    wordpress klo dipake ngetik source code emang selalu dodol

    silent:Ho oh, itu plugin smily bentrok sama syntax highlighter

  • http://beta.ferdianto.com/ ferdhie

    wordpress klo dipake ngetik source code emang selalu dodol

    silent:

    Ho oh, itu plugin smily bentrok sama syntax highlighter