File Browser in PHP Php PHP Projects Tools by Rajesh Kumar Sahanee - February 7, 2017September 11, 20170 Post Views: 6,015 Hello Friends, It’s being long time I’ve not shared anything with you but today I am going to share a php code which is nothing but a file browser code in which you can create folder, upload files, delete files and folders also you can move files and folder to another folder. It is complete basic featured file browser tool. So, Here is the code config.php PHP <?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; } } 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 <?php // This function scans the files folder recursively, and builds a large arrayfunction 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; }} index.php PHP <?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(); } //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="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="https://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 }); } </script> </body> </html> 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 <?phpinclude_once 'config.php'; //uploadingif(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 createif(isset($_REQUEST['createfolder'])) { $foldername = trim($_REQUEST['createfolder']); if(mkdir($dir."/".$foldername, 0777, true)){ header("location: ?path=".$dir); }} //moveif(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();} //deleteif(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();} //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="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 }); } </script> </body></html> Output Download FileBrowser Download 1 file(s) 4.51 KB Download Thanks Please share if you like