Version: 1.0
Type: Full Script
Category: File Management
License: GNU General Public License
Description: A Filesystem script that will allow users to upload, copy, move, delete, rename, download files. Allows you to create and delete directories. Is based on a true filesystem.
<? $temp = "D:/website" . "$PHP_SELF"; //!Define Start Folder for webserver! //Physical Path $path_parts = pathinfo($temp); $currentdir = $path_parts['dirname']; function ByteConverter($bytesize) { $sizearray = array('bytes', 'KBs', 'MBs', 'GBs'); $d = 0; while($bytesize / 1024 > 1 && $d < 3) { $d++; $bytesize /= 1024.0; } $unit = $sizearray[$d]; if($d == 0) return number_format($bytesize) . " " . $unit; else return number_format($bytesize, 2, '.', '') . " " . $unit; } if(!empty($userfile)) //Upload Processor { $userfile_name=basename($userfile_name); if(copy($userfile, $currentdir . "/" . $userfile_name)) { unlink($userfile); echo "File Successfully Uploaded."; } else { echo "Upload Failed..."; } } if(!empty($function)) //Function Control { if($function == "prompt") //Prompt EXEC { if($set == "ren") { $wordarray = array('Rename File:', 'New Filename:', $set, 'Rename', ''); } if($set == "copy") { $wordarray = array('Copy:', 'To:', $set, 'Copy', $currentdir . "/" . $oldname); } if($set == "move") { $wordarray = array('Move:', 'To:', $set, 'Move', $currentdir . "/" . $oldname); } if($set == "newdir") { $wordarray = array('Create A Directory Under:', 'Named:', $set, 'New Directory', ""); $oldname = $currentdir; } echo "<html><body>"; echo "$wordarray[0] $oldname<br>"; echo "<form action= "$PHP_SELF">$wordarray[1] <input type="text" name="newname" size="60" value="$wordarray[4]"><br>"; echo "<input type="hidden" name="function" value="$wordarray[2]">"; echo "<input type="hidden" name="oldname" value="$oldname">"; echo "<input type="submit" name="submit" value="$wordarray[3]"</form>"; echo "</body></html>"; exit; } elseif($function == "ren") //Rename Processor { $oldname = $currentdir . "/" . $oldname; $newname = $currentdir . "/" . $newname; if(rename($oldname,$newname)) { echo "Renamed: "; echo $oldname; echo "<br>To: "; echo $newname; } else { echo "Unable To Rename:"; echo $oldfile; } } elseif($function == "move") //Move Processor { $s1 = copy($currentdir . "/" . $oldname, $newname); if($s1) { $s2 = unlink($currentdir . "/" . $oldname); } if($s1 && $s2) { echo "File has been moved"; } else if($s1 && !$s2) { echo "File has been copied, but original has NOT been deleted"; } else { echo "Errors have occured in the move"; } } elseif($function == "copy") //Copy Processor { if(copy($currentdir . "/" . $oldname, $newname)) { echo "File has been copied"; } else { echo "Copy has failed"; } } elseif($function == "delfile") //File Delete Processor { if(unlink($currentdir . "/" . $file)) { echo "Deleted: "; echo $file; } else { echo "Unable To Delete:"; echo $file; } } elseif($function == "newdir") //New Directory Processor { if(mkdir($currentdir . "/" . $newname, 0700)) { if (copy($currentdir . "/" . "index.php", $currentdir . "/" . $newname . "/" . "index.php")) { echo "Directory has been created"; } else { if(rmdir($currentdir . "/" . $newname)) { echo "Directory creation error: No Index File"; } else { echo "Damn this shit is fucked up"; } } } else { echo "Unable to create directory"; } } elseif($function == "deldir") //Deletes Dir Processor { $dirp = opendir($currentdir . "/" . $dir); $filearray = array(); $filecount = 0; while ($file_name = readdir($dirp)) { if(($file_name != ".") && ($file_name != "..")) { if(is_dir($currentdir . "/" . $dir . "/" . $file_name)) { echo "Fatal Error: All Directories Must Be DELETED From The Subdirectory"; exit; } else { $filearray[$filecount] = $file_name; $filecount++; } } } closedir($dirp); for ($i = 0; $i < $filecount; $i++) { if(unlink($currentdir. "/" . $dir . "/" . $filearray[$i])); else { echo "Unable To Delete: " . $currentdir . "/" . $filearray[$i]; exit; } } if(rmdir($currentdir . "/" . $dir)) { echo "Directory has been deleted"; } else { echo "Directory didnot delete based on some unforseen error"; } } } $dir = opendir("$currentdir"); $dirarray = array(); $filearray = array(); $dircount = 0; $filecount = 0; while ($file_name = readdir($dir)) //Directory Read { if(($file_name != ".") && ($file_name != "..") && ($file_name != "index.php")) { if(is_dir($file_name)) { $dirarray[$dircount] = $file_name; $dircount++; } else { $filearray[$filecount] = $file_name; $filecount++; } } } sort($dirarray); reset($dirarray); sort($filearray); reset($filearray); $msg = "<p><b>Files in: $currentdir</b></p>"; $msg .= "<a href="$PHP_SELF?function=prompt&set=newdir">[New Directory]</a> <a href="../index.php">[Up One Level]</a> <a href="$PHP_SELF?">[Refresh]</a>"; $msg .= "<table cellspacing="4" cellpadding"6">"; for($a = 0; $a < $dircount; $a++) //Output Dir Listing { $msg .= "<tr><td>—</td><td><a href="$dirarray[$a]">$dirarray[$a]</a></td><td><a href="$PHP_SELF?function=deldir&dir=$dirarray[$a]">Delete</a></td></tr>"; } $msg .= "</table><table cellspacing="4" cellpadding"6">"; $msg .= "<tr><td align="center"><b>Filename</b></td><td align="center"><b>Size</b></td><td align="center"><b>Created</b></td><td colspan="4" align="center"><b>Functions</b></td></tr>"; for($b = 0; $b < $filecount; $b++) //Output File Listing { $currentfile = $currentdir . "/" . $filearray[$b]; $del = $PHP_SELF . "?function=delfile&file=" . $filearray[$b]; //Delete UI $size = ByteConverter((double) filesize($currentfile)); //Filesize UI $time = strftime ("%b %d %Y %H:%M:%S", filectime($currentfile)); $rename = "<a href="$PHP_SELF?function=prompt&set=ren&oldname=$filearray[$b]">Rename</a>"; $copy = "<a href ="$PHP_SELF?function=prompt&set=copy&oldname=$filearray[$b]">Copy</a>"; $move = "<a href ="$PHP_SELF?function=prompt&set=move&oldname=$filearray[$b]">Move</a>"; $msg .= "<tr><td><a href="$filearray[$b]">$filearray[$b]</a></td><td align="right">$size</td><td align="right">$time</td><td>$rename</td><td>$copy</td><td>$move</td><td><a href="$del">Delete</a></td></tr>"; } closedir($dir); clearstatcache(); //Output & Upload UI ?> <html> <head> <title>Directory Listing: File Library</title> </head> <body> <? echo $msg;?> <form enctype="multipart/form-data" action="index.php" method=post> <input type="hidden" name="max_file_size" value="2000000"> <input type="hidden" name="option" value="upload"> <tr></tr> <tr></tr> <tr><td colspan="3">Upload File: <input name="userfile" type="file"></td><td align="right" colspan="4"><input type="submit" value="Upload"></td> </tr></table> </form> </body> </html>