Mega Code Archive

 
Categories / Php / File Directory
 

Mirror a directory structure

# This code will mirror a directory structure (like /home/ftp/pub, the anonymous ftp dir) that is headed by the topdir. # The icons it refere to are those provided by the apache server by default. These, of course, can be switched to # whatever you wish. # This is probably not the most elegant way to do this and the ability to differentiate between file types could probably # be added, but it does work. # <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0"> <TR ALIGN="Center" VALIGN="Middle"> <TD ALIGN="Left" VALIGN="Top" WIDTH="5%"> # Change topdir to the name of the top of the directory structure you wish to mirror <IMG SRC="/icons/folder.open.gif" BORDER="0">??<FONT FACE="Arial" SIZE="2"><B><A HREF="files.php3">topdir</A></B></FONT> </TD> </TR> <? // Change this to the path to the top of the directory structure $path = "/path/to/topdir"; // Initialize arrays $darray[0] = "..."; $farray[0] = "..."; // Check to see if script has been called by itself if (isset($selectdir)) { // The selectdir variable contains subdirectories of the topdir separated by //slashes // Explode out these directories and list them out with "open folder icons" if(strstr($selectdir,"/")) { $dirarray = explode("/",$selectdir); } else { $dirarray[0] = $selectdir; } $dirsize = sizeof($dirarray); $i = 0; while ($i < $dirsize) { echo "<TR ALIGN=\"Center\" VALIGN=\"Middle\">\n"; echo "<TD ALIGN=\"Left\" VALIGN=\"Top\">\n"; $j = -1; while ($j < $i) { echo "???"; $j++; } echo "<IMG SRC=\"/icons/folder.open.gif\" BORDER=\"0\">"; echo "??"; echo "<FONT FACE=\"Arial\" SIZE=\"2\"><B><A HREF= \"files.php3?selectdir="; $j = 0; while ($j < $i) { echo $dirarray[$j]."/"; $j++; } echo $dirarray[$i]; echo "\" onMouseover=\"window.status='"; echo $dirarray[$i]; echo "'; return true\" onMouseOut=\"window.status=''; return true\">"; echo $dirarray[$i]; echo "</A></B></FONT>\n"; echo "</TD>\n"; echo "</TR>\n"; $i++; } // Append the selectdir variable on to the path we hardcoded above, resulting // in a full path to the current directory $path = $path."/".$selectdir; } // Open the directory specified in the path variable and read the resulting // entries into two arrays // darray = directories // farray = all others (files) $dir=opendir($path); while ($file=readdir($dir)) { if ($file!="." && $file!=".." && $file!="incoming") { switch (filetype($path."/".$file)) { case 'dir': $darray[] = $file; break; default: $farray[] = $file; break; } } } closedir($dir); // Sort the arrays by name sort($darray); sort($farray); // List out the subdirectories of the current directory $asize = sizeof($darray); $i = 0; while ($i < $asize) { if ($darray[$i] != "...") { echo "<TR ALIGN=\"Center\" VALIGN=\"Middle\">\n"; echo "<TD ALIGN=\"Left\" VALIGN=\"Top\">\n"; $j = -1; while ($j < $dirsize) { echo "???"; $j++; } echo "<IMG SRC=\"/icons/dir.gif\" BORDER=\"0\">"; echo "??"; echo "<FONT FACE=\"Arial\" SIZE=\"2\"><B><A HREF= \"files.php3?selectdir="; if (isset($selectdir)){ echo $selectdir."/".$darray[$i]; } else { echo $darray[$i]; } echo "\" onMouseover=\"window.status='"; echo $darray[$i]; echo "'; return true\" onMouseOut=\"window.status=''; return true\">"; echo $darray[$i]; echo "</A></B></FONT>\n"; echo "</TD>\n"; echo "</TR>\n"; } $i++; } // List out the files in the current directory $asize = sizeof($farray); $i = 0; while ($i < $asize) { if ($farray[$i] != "...") { echo "<TR ALIGN=\"Center\" VALIGN=\"Middle\">\n"; echo "<TD ALIGN=\"Left\" VALIGN=\"Top\">\n"; $j = -1; while ($j < $dirsize) { echo "???"; $j++; } echo "<IMG SRC=\"/icons/generic.gif\" BORDER=\"0\">"; echo "??"; echo "<FONT FACE=\"Arial\" SIZE=\"2\"><B><A HREF= \"ftp://ftp.yps.org/pub/"; if (isset($selectdir)){ echo $selectdir."/".$farray[$i]; } else { echo $farray[$i]; } echo "\" onMouseover=\"window.status='"; echo $farray[$i]; echo "'; return true\" onMouseOut=\"window.status=''; return true\">"; echo $farray[$i]; echo "</A></B></FONT>\n"; echo "</TD>\n"; echo "</TR>\n"; } $i++; } ?> </TABLE>