I’m a newbie when it comes to php, so I decided to create a little project for myself to kind of start to learn the language, a script that creates a file list of the contents of a folder, displays them as links with icons according to what file type they are. I have done that and it works great except it doesn’t recognize what is a folder sometimes in subdirectories.
The Code:
<title>Seth's Server</title>
<style type="text/css">
<!--
.title {
font-family: Arial, Helvetica, sans-serif;
font-size: large;
font-weight: bold;
}
a:link {
color: #666666;
text-decoration: none;
border: none;
outline: none;
}
a:visited {
text-decoration: none;
color: #666666;
border: none;
}
a:hover {
text-decoration: none;
color: #999999;
border: none;
}
a:active {
text-decoration: none;
border: none;
}
.normal {
font-family: Arial, Helvetica, sans-serif;
font-size: small;
font-style: normal;
color: #000000;
text-decoration: none;
border: none #000000;
}
-->
</style>
<?php
$folder = $_GET['folder'];
echo "<div class='title'>My Server:</div>";
function ls ($curpath) {
if($curpath == ""){
$curpath = ".";
}
$handle = opendir($curpath);
echo("<b>$folder</b>");
echo "<blockquote>";
while ($file = readdir($handle)) {
if($curpath != "." && $file == ".."){
$icon = "left-orange.gif";
$iconPath = "/images/icons/".$icon;
echo("<a class='normal' href='".$PHP_SELF."?folder='..'><img src='".$iconPath."' border='0'> ROOT</a><br>");
}
if($file != "." && $file != "..") {
if (is_dir($file)) {
$icon = "folder-orange.gif";
$iconPath = "/images/icons/".$icon;
//$curpath = $curpath."/".$file;
echo("<a class='normal' href='".$PHP_SELF."?folder=".$curpath."/".$file."'><img src='".$iconPath."' border='0'> ".$file."</a><br>");
//ls($curpath);
}
}
}
closedir($handle);
$handle = opendir($curpath);
while ($file = readdir($handle)) {
if($file != "." && $file != ".." && $file != ".htaccess") {
if (is_dir($file)) {
}else{
$fileInfo = pathinfo($file);
$extension = $fileInfo['extension'];
$icon = getIcon($extension);
$iconPath = "/images/icons/".$icon;
echo("<a style='normal' href='".$curpath."/".$file."'><img src='".$iconPath."' border='0'> ".$file."<br>");
}
}
}
closedir($handle);
echo "</blockquote>";
return;
}
function getIcon($extension){
$extension = strtolower($extension);
switch ($extension){
case "php":
return "web-page-green.gif";
break;
case "jpg":
return "jpg.gif";
break;
case "bmp":
return "bmp.gif";
break;
case "png":
return "png.gif";
break;
case "gif":
return "gif.gif";
break;
case "mp3":
return "mp3-music-file.gif";
break;
}
return "file.gif";
}
$startpath = $folder;
ls($startpath);
?>
Thank you for any help or insight you could provide.