Php Programming Code Examples
Php > File Manipulation Code Examples
Directory browser
Directory browser
<?php
if($pageAction=="add")
{
chdir(".".$dirname);
mkdir($directory,0);
Header("Location: index.php3?dirname=$dirname");
}
if($pageAction=="delete")
{
chdir(".".$dirname);
rmdir($directory);
Header("Location: index.php3?dirname=$dirname");
}
?>
<html>
<head>
<title>Directory Browser</title>
</head>
<body>
<?php
echo "<table width=\"600\" border=\"0\">";
if($dirname)
{
$myDirectory=opendir(".".$dirname);
chdir(".".$dirname);
echo "<tr>";
echo "<td width=\"60\">";
echo "<table width='60' border='0'>";
echo "<tr valign='center'><td align='center'>";
$list=explode("/", $dirname);
for($i=1;$i<sizeof($list)-1;$i++)
{
$olddir=$olddir . "/" . $list[$i];
}
echo "<a href='index.php3?dirname=$olddir'><img border='0'
src='http://localhost/DirAcc/dirback.gif'></a>";
echo "<br>";
echo "<a href='index.php3?dirname=$olddir'><font face='tahoma'
size='1'>Back</font></a><br>";
echo "</tr></td>";
echo "</table>";
echo "</td>";
$dir_count=1;
$dir_count_tot=1;
}
else
{
$myDirectory=opendir(".");
$dir_count=0;
$dir_count_tot=0;
}
while($entryName=readdir($myDirectory))
{
if(!is_file($entryName))
{
if($entryName!=".." && $entryName!=".")
{
if($dir_count==0)
{
echo "<tr>";
}
echo "<td width=\"60\">";
$toopen=$dirname . "/" . $entryName;
echo "<table width=\"60\" border=\"0\"><tr><td
align=\"center\">";
$dirs[]=$entryName;
echo "<a href=\"index.php3?dirname=$toopen\"><img
border=\"0\" src=\"http://localhost/NewDirAcc/dir.gif\"></a><br>";
echo "<a href=\"index.php3?dirname=$toopen\"><font
face=\"tahoma\" size=\"1\">$entryName</font></a><br>";
echo "</td></tr></table>";
echo "</td>";
$dir_count++;
$dir_count_tot++;
if($dir_count==10)
{
$dir_count=0;
echo "</tr>";
}
}
}
else
{
if(strstr($entryName, ".gif") || strstr($entryName, ".gif"))
{
if($entryName!="dir.gif" && $entryName!="dirback.gif")
{
$images[]=$entryName;
}
}
}
}
$i=0;
while($i<count($images))
{
if($dir_count==0)
{
echo "<tr>";
}
echo "<td width=\"60\">";
$toopen=$dirname . "/" . $images[$i];
echo "<table width=\"60\" border=\"0\"><tr><td align=\"center\">";
$theImg="/NewDirAcc" . $dirname . "/" . $images[$i];
echo "<a href=\"$theImg\"><img border=\"0\" src=\"$theImg\" width=\"30\"
height=\"30\"></a><br>";
echo "<a href=\"$theImg\"><font face=\"tahoma\" size=\"1\">$images[$i]
</font></a><br>";
echo "</td></tr></table>";
echo "</td>";
$dir_count++;
$dir_count_tot++;
if($dir_count==10)
{
$dir_count=0;
echo "</tr>";
}
$i++;
}
while($dir_count_tot<10)
{
echo "<td width=\"60\">";
echo "<table width=\"60\" border=\"0\"><tr><td align=\"center\">";
echo "<br>";
echo "</td></tr></table>";
echo "</td>";
$dir_count_tot++;
}
echo "</tr>";
echo "</table>";
echo "<p>";
echo "<hr width=\"650\" align=\"left\">";
echo "<p>";
?>
<?php echo "<form action=\"index.php3?dirname=$dirname\" method=\"post\">"; ?>
<input type="hidden" name="pageAction" value="add">
Add Directory:
<input name="directory" type="text">
<input type="submit" value="Create">
</form>
<?php echo "<form action=\"index.php3?dirname=$dirname\" method=\"post\">"; ?>
<input type="hidden" name="pageAction" value="delete">
Delete Directory:
<select name="directory">
<option selected value="none">Please Select</option>
<?php
$i=0;
while($i<count($dirs))
{
echo "<option value=\"$dirs[$i]\">$dirs[$i]</option>";
$i++;
}
?>
</select>
<input type="submit" value="Delete">
</form>
<form>
Image:
<input type="text">
<input type="submit" value="Upload"> Not Working Yet!
</form>
to do: Also read directories into an array first like with the images. Then we can list
alphabetically
too!!! Need to do the upload (simple) then thats pretty much it...
</body>
</html>