|
Posted by Tiz on 12/22/07 01:15
code review request
A couple of days ago, I was dismayed to find my ~/public_html directory
choked with junk files. I wanted a micro file manager to be able to
parse the files and directories quickly and it seems like php is the
answer.
I found a public domain script on www.phptoys.com called
microFileManager.php that I based the following code. The original code
used tables. The code below uses a style sheet and presents the
directory listing in column 1 and the selected file content in column 2
in a basic menu/content 2 column format.
Sample style sheet provided.
I'm new to php and would appreciate feedback on this script. Layout,
function calls, error handling - whatever. Perhaps there are php classes
or functions that can do this better.
The micro file manager requirements are...
o create a well written php script
o not to exceed 100 lines of code
o wrapping at column 80 - personal preference - posting to
newsgroup would make this requirement difficult at best -
so perhaps this should read - readable code.
o directory listings displayed on left column
o selecting item in listing displays item in right column
o two files only, the script and the stylesheet. No extra
gifs, functions, features.
o no tables
CODE:
<?php
function showHeader($path) {
print "<div id='Header'>";
print "Path: $path";
print "</div>";
}
function showMenu($path) {
if ($handle = opendir($path))
{
print "<div id='Menu'>";
print "<h2>Menu</h2>";
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
print "<a href='".$_SERVER['PHP_SELF']."?path=$up'>Up one level</
a>";
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) {
print "<a href=";
print "'".$_SERVER['PHP_SELF']."?path=
$path&file=$fName'";
print "=>$fName</a>";
} elseif (is_dir($file)) {
print "<a href=";
print "'".$_SERVER['PHP_SELF']."?path=
$file'>$fName</a>";
}
}
print "<br>";
}
print "</div>";
closedir($handle);
}
}
function showContent($path, $fn){
print "<div id='Content'>";
print "<h2>$fn</h2>";
$imageext = array(jpg, jpeg, JPEG, JPG, gif, png);
$textext = array(txt, cfg, sql, css, plugin);
$path_parts = pathinfo($fn);
if (in_array($path_parts['extension'], $imageext, true)) {
echo '<img src="' . $path . "/" . $fn . '">';
} elseif (in_array($path_parts['extension'], $textext, true)) {
echo '<pre>';
include("$path/$fn");
echo '</pre>';
} else {
include("$path/$fn");
}
print "</div>";
}
if (isset($_POST['submitBtn'])){
$actpath = isset($_POST['path']) ? $_POST['path'] : '.';
} else {
$actpath = isset($_GET['path']) ? $_GET['path'] : '.';
}
if (isset ($_GET['file']) )
{
$filename = $_GET['file'];
}
showHeader($actpath);
showMenu($actpath);
if (isset($filename)){
showContent($actpath, $filename);
}
?>
CODE:
body {
margin:0px;
padding:0px;
font-family:verdana, arial, helvetica, sans-serif;
color:#333;
background-color:white;
}
h1 {
margin:1px 0px 15px 0px;
padding:0px;
font-size:18px;
line-height:28px;
font-weight:900;
color:#ccc;
}
p {
font:11px verdana, arial, helvetica, sans-serif;
margin: -10px 0px 0px 0px;
}
#Menu h2
{
color:#99A575;
font-size:13px;
text-decoration:none;
font-weight:600;
font-family:verdana, arial, helvetica, sans-serif;
}
a {
color:#99A575;
font-size:11px;
text-decoration:none;
font-family:verdana, arial, helvetica, sans-serif;
}
a:link {color:#99A579;}
a:visited {color:#93604f;}
a:hover {background-color:#eeeeee;}
#Header {
margin:20px 0px 10px 210px; /* space from top, width, height,
inside margin */
padding:17px 0px 0px 10px;
border-style:solid;
border-color:black;
border-width:1px 1px; /* top and bottom borders, left and right
borders */
line-height:11px;
background-color:#eeeeee;
voice-family: "\"}\"";
voice-family:inherit;
}
body>#Header {height:14px;}
#Content {
margin:0px 50px 50px 220px;
padding:10px;
font:11px/20px verdana, arial, helvetica, sans-serif;
}
#Menu {
position:absolute;
top:20px;
left:20px;
padding:10px;
background-color:#EFE9C4; /* Menu background */
border:1px;
dashed #999;
line-height:17px;
voice-family: "\"}\"";
voice-family:inherit;
border-style:solid;
}
body>#Menu {width:170px;} /* Menu width */
CODE: header it insert style sheet.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>File Browser</title>
<link href="style/layoutdir.css" rel="stylesheet" type="text/
css" />
</head>
<body>
replace this line with php code
</body>
[Back to original message]
|