Hello Friends, today I am going to share updated php code forĀ File Browser in which I’ve added rename option to rename file and folder. So, Here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php // This function scans the files folder recursively, and builds a large array function scan($dir) { $files = array(); // Is there actually such a folder/file? if (file_exists($dir)) { foreach (scandir($dir) as $f) { if (!$f || $f[0] == '.') { continue; // Ignore hidden files } if (is_dir($dir . '/' . $f)) { // The path is a folder $files[] = array( "name" => $f, "type" => "folder", "path" => $dir . '/' . $f, "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder ); } else { // It is a file $files[] = array( "name" => $f, "type" => "file", "path" => $dir . '/' . $f, "size" => filesize($dir . '/' . $f) // Gets the size of this file ); } } } return $files; } function upload($file, $filesize, $directory, $overwrite) { $filename = trim($_FILES[$file]["name"]); $response['filename'] = ""; $response['error'] = true; //assuming there is an error $response['error_msg'] = ""; if (empty($filename)) { $response['error_msg'] = "File not selected"; return $response; } if ($_FILES[$file]["error"] > 0) { $response['error_msg'] = 'There is some problem please try again later.' . $_FILES[$file]["error"]; return $response; } //file size limit checking if (($filesize * 1024) < $_FILES[$file]["size"]) { $response['error_msg'] = 'File size should less than ' . $filesize . ' kb.'; return $response; } //if everything alright if ($directory != null && $directory <> '') { $directory = $directory . "/"; } else { $directory = ""; } //if overwrite parameter is not Y then new file name needs to be generated if ($overwrite != 'Y') { $i = 1; while (file_exists($directory . $filename)) { $filename = basename($filename, "." . $extension) . "_" . $i . "." . $extension; $i++; } } if (move_uploaded_file($_FILES[$file]["tmp_name"], $directory . $filename)) { $response['filename'] = basename($filename); $response['error'] = false; return $response; } else { $response['error_msg'] = "File could not moved"; return $response; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
<?php include_once 'config.php'; //uploading if(isset($_FILES['file']) && isset($_REQUEST['path'])) { $fileElement = "file"; $path = urldecode($_REQUEST['path']); $response = upload($fileElement, 10000, $path, "N"); if(!$response['error']) { echo "success"; } exit(); } $dir = "files"; if(isset($_REQUEST['path'])) { $dir = urldecode($_REQUEST['path']); } //folder create if(isset($_REQUEST['createfolder'])) { $foldername = trim($_REQUEST['createfolder']); if(mkdir($dir."/".$foldername, 0777, true)){ header("location: ?path=".$dir); } } //move if(isset($_REQUEST['move']) && isset($_REQUEST['filenames']) && isset($_REQUEST['folder'])) { $filenames = $_REQUEST['filenames']; $folder = $_REQUEST['folder']; $currentdir = urldecode($_REQUEST['path']); $flag = true; foreach($filenames as $filename) { //echo $currentdir . "/" . $filename . "=" . $folder . "/" . $filename; if (!rename($currentdir . "/" . $filename, $folder . "/" . $filename)) { $flag = false; break; } } if($flag) { echo "success"; } exit(); } //delete if(isset($_REQUEST['delete']) && isset($_REQUEST['filenames'])) { $filenames = $_REQUEST['filenames']; $currentdir = urldecode($_REQUEST['path']); $flag = true; foreach($filenames as $filename) { //delete if it is directory if(is_dir($currentdir . "/" . $filename)) { rmdir($currentdir . "/" . $filename); } if (is_file($currentdir . "/" . $filename) && !unlink($currentdir . "/" . $filename)) { $flag = false; break; } } if($flag) { echo "success"; } exit(); } //rename if(isset($_REQUEST['rename']) && isset($_REQUEST['filename'])) { $filename = $_REQUEST['filename']; $currentdir = urldecode($_REQUEST['path']); $newfilename = $_REQUEST['newfilename']; if (!file_exists($currentdir . "/" . $newfilename)) { if (rename($currentdir . "/" . $filename, $currentdir . "/" . $newfilename)) { echo "success"; } } else { echo "exists"; } exit(); } //files and folder list $files = scan($dir); ?> <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>File Browser</title> <!-- Include our stylesheet --> <link href='//fonts.googleapis.com/css?family=Questrial' rel='stylesheet' type='text/css'> <link href='//fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' rel='stylesheet' type='text/css'> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <style> .selected { background-color: rgba(192, 192, 192, 0.6); } .file-upload { cursor: inherit; display: block; /*font-size: 999px;*/ filter: alpha(opacity=0); min-height: 100%; min-width: 100%; opacity: 0; position: absolute; right: 0; text-align: right; top: 0; } </style> </head> <body> <div class="container" style="margin-top: 20px;"> <div class="panel panel-default"> <div class="panel-heading"> File Browser <div class="pull-right"> <span id="uploading" style="display: none;">Uploading...</span> <div class="btn-group"> <button class="btn btn-default btn-sm" title="Upload"><i class="fa fa-upload"></i> Upload<input type="file" class="file-upload"/></button> <a href="javascript:void()" onclick="downloadSelected()" class="btn btn-sm btn-default" title="Download"><i class="fa fa-download"></i> Download</a> <a href="javascript:void()" onclick="renameSelected()" class="btn btn-sm btn-default" title="Rename"><i class="fa fa-pencil"></i> Rename</a> <a href="javascript:void()" onclick="moveSelected()" class="btn btn-sm btn-default" title="Move"><i class="fa fa-arrows"></i> Move</a> <a href="javascript:deleteSelected()" onclick="return confirm('are you sure, you want to delete?')" class="btn btn-sm btn-danger" title="Delete"><i class="fa fa-trash"></i> Delete</a> <a href="javascript:void()" onclick="createFolder()" class="btn btn-sm btn-primary" title="Create New Folder"><i class="fa fa-folder"></i> New Folder</a> </div> </div> <p style="font-size: 10px;font-weight: bold; color: black;"><?php echo $dir; ?></p> </div> <div class="panel-body"> <div class="btn-group"> <a href="javascript:void()" onclick="javascript:window.history.back()" class="btn btn-sm btn-default">Back</a> <a href="javascript:void()" onclick="javascript:window.history.forward()" class="btn btn-sm btn-default">Forward</a> </div> <div class="table-responsive"> <table class="table table-condensed table-hover"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Size</th> </tr> </thead> <tbody> <?php foreach ($files as $file) { $path = "?path=".urlencode($file['path']); if($file['type'] == "file") { $path = $file['path']; } ?> <tr> <td><a href="<?php echo $path; ?>" <?php if($file['type'] == "file") { ?> target="_blank" download <?php } ?>><?php echo $file['name']; ?></a></td> <td><?php echo $file['type']; ?></td> <td><?php if($file['type'] == "file") { echo $file['size']; } ?></td> </tr> <?php } ?> </tbody> </table> </div> </div> </div> </div> <!-- Include our script files --> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> <script> $("input[type=file]").change(function(){ var action = "<?php echo basename($_SERVER['PHP_SELF']) . "?path=". urlencode($dir); ?>"; if($(this).val() === "") { return; } $("#uploading").show(); var data = new FormData(); data.append("file", $('input[type=file]')[0].files[0]); $.ajax({ type: 'POST', url: action, data: data, /*THIS MUST BE DONE FOR FILE UPLOADING*/ contentType: false, processData: false, }).done(function(data){ $("#uploading").hide(); if(data === "success") { window.location.href = "?path=<?php echo $dir; ?>"; } }).fail(function(data){ //any message }); }); function createFolder() { var foldername = prompt("Enter Folder Name", "New Folder"); if(foldername !== null) { window.location.href = "?path=<?php echo $dir; ?>&createfolder=" + foldername; } } cntrlIsPressed = false; $(document).keydown(function(event){ if(event.which=="17") cntrlIsPressed = true; }); $(document).keyup(function(){ cntrlIsPressed = false; }); $("table tbody tr").hover(function(){ //$(this).addClass('active').siblings().removeClass('active'); }); $("table tbody tr").click(function(){ if(cntrlIsPressed) { $(this).addClass('selected'); } else { $(this).addClass('selected').removeClass('active').siblings().removeClass('selected'); } }); $('.ok').on('click', function(e){ alert($("#table tr.selected td:first").html()); var selectedIDs = []; $("#table tr.selected").each(function(index, row) { selectedIDs.push($(row).find("td:first").html()); }); }); function downloadSelected(){ $("table tbody tr.selected").each(function () { var url = $(this).find("a").attr("href"); if($(this).find("td:eq(1)").html() === "file") { var a = $("<a>").attr("href", url).attr("download", "").appendTo("body"); a[0].click(); a.remove(); } }); } function moveSelected(){ var action = "<?php echo basename($_SERVER['PHP_SELF']) . "?path=". urlencode($dir); ?>"; if($("table tbody tr.selected").length <= 0) { alert("Please select file/s"); return; } var folder = prompt("Enter Folder Name to Move","<?php echo $dir; ?>"); var data = new FormData(); data.append("move", "1"); data.append("folder", folder); $("table tbody tr.selected").each(function (index) { var filename = $(this).find("td:eq(0)").text(); data.append("filenames["+index+"]", filename); }); $.ajax({ type: 'POST', url: action, data: data, /*THIS MUST BE DONE FOR FILE UPLOADING*/ contentType: false, processData: false, }).done(function(data){ if(data.trim() === "success") { window.location.href = "?path=<?php echo $dir; ?>"; } }).fail(function(data){ //any message }); } function deleteSelected(){ var action = "<?php echo basename($_SERVER['PHP_SELF']) . "?path=". urlencode($dir); ?>"; if($("table tbody tr.selected").length <= 0) { alert("Please select file/s"); return; } var data = new FormData(); data.append("delete", "1"); $("table tbody tr.selected").each(function (index) { var filename = $(this).find("td:eq(0)").text(); data.append("filenames["+index+"]", filename); }); $.ajax({ type: 'POST', url: action, data: data, /*THIS MUST BE DONE FOR FILE UPLOADING*/ contentType: false, processData: false, }).done(function(data){ if(data.trim() === "success") { window.location.href = "?path=<?php echo $dir; ?>"; } }).fail(function(data){ //any message }); } function renameSelected(){ var action = "<?php echo basename($_SERVER['PHP_SELF']) . "?path=". urlencode($dir); ?>"; if($("table tbody tr.selected").length <= 0) { alert("Please select file"); return; } if($("table tbody tr.selected").length > 1) { alert("Only one file can be renamed at once"); return; } var newfilename = prompt("Enter New File Name",""); var data = new FormData(); data.append("rename", "1"); data.append("newfilename", newfilename); $("table tbody tr.selected").each(function (index) { var filename = $(this).find("td:eq(0)").text(); data.append("filename", filename); }); $.ajax({ type: 'POST', url: action, data: data, /*THIS MUST BE DONE FOR FILE UPLOADING*/ contentType: false, processData: false, }).done(function(data){ if(data.trim() === "success") { window.location.href = "?path=<?php echo $dir; ?>"; } if(data.trim() === "exists") { alert("filename already exists"); } }).fail(function(data){ //any message }); } </script> </body> </html> |
Output
Download
FileBrowser Updated Download
1 file(s) 11.79 KB
Thanks
Please share if you like
Comments