Last active
March 30, 2016 16:29
-
-
Save michvaldes001/90f6a049be418b184b99baed4765caf5 to your computer and use it in GitHub Desktop.
A PHP based file manager back end.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Change directory based on input from URL. | |
function change_directory() | |
{ | |
//If a directory variable is present in URL, change directory to match. | |
if (isset($_GET['dir_change'])) | |
{ | |
$directory_to_change = $_GET['dir_change']; | |
chdir($directory_to_change); | |
} | |
//If no directory variable is present in URL, change directory to defauly value. | |
else | |
{ | |
chdir(ROOT_DIRECTORY); | |
} | |
} | |
//Refresh page. | |
function refresh() | |
{ | |
echo '<META HTTP-EQUIV="Refresh" Content="1; URL="<YOUR URL PATH TO FILE>'; | |
} | |
//List contents of a folder. | |
function list_directory() | |
{ | |
echo '<h3>Actions: </h3>'; | |
//Form to set check boxes and radio buttons. | |
echo '<form name = "file_actions" method = "POST">'; | |
//Radio buttons. Select file operation to perform. Default to "edit". | |
echo '<input type = "radio" name = "file_action" value = "delete">Delete  '; | |
echo '<input type = "radio" name = "file_action" value = "edit" checked = "checked">Edit   '; | |
echo '<input type = "radio" name = "file_action" value = "rename">Rename   '; | |
echo '<input type = "radio" name = "file_action" value = "new">New File   '; | |
echo '<input type = "radio" name = "file_action" value = "copy_move">Copy or Move File   '; | |
echo '<input type = "radio" name = "file_action" value = "upload">Upload   '; | |
echo '<input type = "submit" name = "modify_file">'; | |
//Set file list variable to current working folder. | |
$file_list = scandir(getcwd()); | |
//Start file list. | |
echo '<ul style = "list-style-type: none;">'; | |
//Display all files in directory one by one. | |
foreach($file_list as $files) | |
{ | |
//Ignore the '.' (or current directory folder). | |
if($files != '.') | |
{ | |
echo '<li><input type = "checkbox" name = "file_select[]" value = "' . getcwd() . '/' .$files . '">'; | |
//Check if file is directory. Set link change directory variable accordingly. | |
if (is_dir($files)) | |
{ | |
echo '<a href = "<YOUR URL PATH TO FILE>&dir_change=' . getcwd() . '/'. $files . '">' . $files. '</a>'; | |
} | |
//Check if file is an image. Display link to image. | |
elseif (substr(strrchr($files,'.'),1) == "jpg" || substr(strrchr($files,'.'),1) == "JPG" || substr(strrchr($files,'.'),1) == "png" || substr(strrchr($files,'.'),1) == "PNG") | |
{ | |
//Remove root default directory from working directory string. Absolute path will not work in URL. | |
echo '<a href = "'. str_replace(ROOT_DIRECTORY, "", getcwd()) . '/' . $files . '">' . $files . '</a>'; | |
} | |
//Check if file is a text file. Display link to text file. | |
elseif (substr(strrchr($files,'.'),1) == "txt") | |
{ | |
//Remove root default directory from working directory string. Absolute path will not work in URL. | |
echo '<a href = "'. str_replace(ROOT_DIRECTORY, "", getcwd()) . '/' . $files . '">' . $files . '</a>'; | |
} | |
//By defauly, display just the file names. | |
else | |
{ | |
echo $files; | |
} | |
echo '</li>'; | |
} | |
} | |
//End list and form. | |
echo '</ul>'; | |
echo '</form>'; | |
//If the submit button is pressed, execute following actions. | |
if (isset($_POST['modify_file'])) | |
{ | |
//If the upload radio button is selected, set the session variable 'editor_activate' to 3. | |
//This will cause the upload function to be called when page refreshes. | |
//Refresh page when done. | |
if ($_POST['file_action'] == 'upload') | |
{ | |
$_SESSION['editor_activate'] = 3; | |
echo '<h3>Entering upload screen...</h3>'; | |
refresh(); | |
} | |
//If the upload radio button is selected, set the session variable 'editor_activate' to 4. | |
//This will cause the file file function to be called when page refreshes. | |
//Refresh page when done. | |
if ($_POST['file_action'] == 'new') | |
{ | |
$_SESSION['editor_activate'] = 4; | |
echo '<h3>Entering file create screen...</h3>'; | |
refresh(); | |
} | |
//Detect all the files who's check boxes have been selected. | |
foreach($_POST['file_select'] as $selected_list) | |
{ | |
//Count how many boxed have been checked. | |
$check_count++; | |
//If edit radio button is selected and only one check box has been selected, set session variable 'working_selected_file' to selected file. // Set session 'editor_activate" variable to one. This calls the editor function when page is refreshed. | |
//Refresh page when done. | |
if ($_POST['file_action'] == 'edit' && $check_count == 1) | |
{ | |
$_SESSION['working_selected_file'] = $selected_list; | |
$_SESSION['editor_activate'] = 1; | |
echo '<h3>Editing:' . $selected_list . '...</h3>'; | |
refresh(); | |
} | |
//If edit radio button is selected, and more than one file is checked, display error message. | |
//Refresh page when done. | |
elseif ($_POST['file_action'] == 'edit' && $check_count > 1) | |
{ | |
echo '<h3>Cannot edit more than one file at a time! Only first checked file will be used.</h3>'; | |
} | |
//If delete radio button is selected, call the delete file function. | |
//Refresh page when done | |
elseif ($_POST['file_action'] == 'delete') | |
{ | |
delete_file($selected_list); | |
refresh(); | |
} | |
//If rename radio button is selected and only one box is checked, set session 'working_selected_file' variable to selected file. | |
//Set session 'editor_activate' variable to 2. This will call the rename function when page is refreshed. | |
//Refresh page when done. | |
elseif ($_POST['file_action'] == 'rename' && $check_count == 1) | |
{ | |
$_SESSION['working_selected_file'] = $selected_list; | |
$_SESSION['editor_activate'] = 2; | |
echo '<h3>Entering rename screen...</h3>'; | |
refresh(); | |
} | |
//If rename radio button is selected and more than one box is checked, diaplay error messahe. | |
//Refresh page when done. | |
elseif ($_POST['file_action'] == 'rename' && $check_count > 1) | |
{ | |
echo '<h3>Cannot rename more than one file at a time!</h3>'; | |
refresh(); | |
} | |
//If copy move radio button is selected, set session 'working_selected_file' to selected file. | |
//Set session 'editor_activate' variable to 5. This will call the copy move function when page is refreshed. | |
//Refresh page when done. | |
elseif ($_POST['file_action'] == 'copy_move') | |
{ | |
$_SESSION['working_selected_file'] = $selected_list; | |
$_SESSION['editor_activate'] = 5; | |
echo '<h3>Entering Copy Move Screen...</h3>'; | |
refresh(); | |
} | |
echo '<br>'; | |
} | |
} | |
} | |
//Editor function. | |
function editor($get_file) | |
{ | |
//Get contents of working file. | |
$working_file = file_get_contents($get_file); | |
//Create input text box and put contents of file in it. | |
echo '<br><form name = "editor_form" method = "POST">'; | |
echo '<textarea id = "editor" name = "editor" rows = "50" cols = "100">' . $working_file . '</textarea><br><br>'; | |
echo '<input type = "submit" name = "edit_file"></form>'; | |
//If submit button is pressed, edit contents of file based on textbox input. | |
//Reset session 'editor_activate' variable. | |
//Refresh page when done. | |
if (isset($_POST['edit_file'])) | |
{ | |
file_put_contents($get_file, $_POST['editor']); | |
$_SESSION['editor_activate'] = 0; | |
refresh(); | |
} | |
} | |
//Delete file function. | |
function delete_file($get_file) | |
{ | |
echo '<h3>Deleting file: ' . $get_file . '</h3>'; | |
//If file is a folder, use the rmdir function. | |
if (is_dir($get_file)) | |
{ | |
rmdir($get_file); | |
} | |
//If file is not a folder, use the unlink function. | |
else | |
{ | |
unlink($get_file); | |
} | |
} | |
//Rename file function. | |
function rename_file($get_file) | |
{ | |
echo '<br>'; | |
//Create form and text input to create new filename. | |
//Display current name of file. | |
echo '<h3>Renaming file:' . $get_file . '</h3>'; | |
echo '<form name = "rename" id = "rename" method = "POST">'; | |
echo '<input type = "text" name = "rename_file" id = "rename_file"><br><br>'; | |
echo '<input type = "submit" name = "rename_file_submit" id = "rename_file_submit"></form><br>'; | |
//if Submit button us pressed, rename file based on text input. | |
//Reset session 'editor_activate' variable. | |
//Refresh page when done. | |
if (isset($_POST['rename_file_submit'])) | |
{ | |
rename($get_file, (getcwd() . '/' . $_POST['rename_file'])); | |
echo '<h3>Renaming to: ' . getcwd() . '/' . $_POST['rename_file'] . '</h3>'; | |
$_SESSION['editor_activate'] = 0; | |
refresh(); | |
} | |
} | |
//File upload function. | |
function upload_file($get_file) | |
{ | |
//Display current directory where file will be uploaded. | |
echo '<h3>Uploading to directory: ' . getcwd() . '<h3>'; | |
//Create file upload form and button. | |
echo '<form name = "upload_file" method = "POST" enctype="multipart/form-data"><br><br>'; | |
echo '<input type = "file" name = "file_upload"><br><br>'; | |
echo '<input type = "submit" name = "upload_submit"></form>'; | |
//If submit button is pressed, upload the file to temp and then move file to working directory. | |
//Reset session 'editor_activate' variable. | |
//Refresh when done. | |
if (isset($_POST['upload_submit'])) | |
{ | |
$upload_file_name = $_FILES['file_upload']['name']; | |
$upload_file_temp_name = $_FILES['file_upload']['tmp_name']; | |
move_uploaded_file($upload_file_temp_name, (getcwd() . '/' . $upload_file_name)); | |
$_SESSION['editor_activate'] = 0; | |
refresh(); | |
} | |
} | |
function new_file($get_file) | |
{ | |
echo '<br>'; | |
//Display directory where file will be created in. | |
echo '<h3>Creating file in" ' . getcwd() . '</h3>'; | |
//Create form with text box to input file name. | |
echo '<form name = "create" id = "create" method = "POST">'; | |
//Create radio buttons to choose between creating a file or folder. | |
echo '<input type = "radio" name = "file_create" value = "file">Create File   '; | |
echo '<input type = "radio" name = "file_create" value = "folder">Create Folder   '; | |
echo '<input type = "text" name = "create_file" id = "create_file"><br><br>'; | |
echo '<input type = "submit" name = "create_file_submit" id = "create_file_submit"></form><br>'; | |
//If submit button is pressed, create file or folder. | |
//Reset session 'editor_activate' variable. | |
//Refresh page when done. | |
if (isset($_POST['create_file_submit'])) | |
{ | |
//If create file radio button is selected, create new file. | |
if ($_POST['file_create'] == 'file') | |
{ | |
$new_file_name = $_POST['create_file']; | |
$new_file_handle = fopen($new_file_name, 'w'); | |
fclose($new_file_handle); | |
echo '<h3>Creating file: ' . getcwd() . '/' . $_POST['create_file'] . '</h3>'; | |
} | |
//If create folder radio button is selected, create a new folder. | |
elseif ($_POST['file_create'] == 'folder') | |
{ | |
//NOTE, when implementing this code, do not grant folder full permissions. I did this for testing purposes. | |
mkdir(getcwd() . '/' . $_POST['create_file'], 0777); | |
} | |
$_SESSION['editor_activate'] = 0; | |
refresh(); | |
} | |
} | |
function copy_move_file($get_file) | |
{ | |
$file_name_only = array_pop(explode('/', $get_file)); | |
echo '<h3>File to be copied or moved: ' . $get_file . '</h3>'; | |
echo '<form name = "file_copy_move" method = "POST">'; | |
//Radio buttons. Select file operation to perform. Default to copy or move. | |
echo '<input type = "radio" name = "file_copy_move" value = "copy">Copy to  '; | |
echo '<input type = "radio" name = "file_copy_move" value = "move">Move to   '; | |
echo '<input type = "submit" name = "copy_move_file">'; | |
//Set file list variable to current working folder. | |
$file_list = scandir(getcwd()); | |
//Start file list. | |
echo '<ul>'; | |
//Display all folders in directory one by one. | |
//This will set the destination folder for file to be copied or moved. | |
foreach($file_list as $files) | |
{ | |
//Ignore the '.' (or current directory folder). | |
if($files != '.') | |
{ | |
if (is_dir($files)) | |
{ | |
echo '<li><a href = "<YOUR URL PATH TO FILE>&dir_change=' . getcwd() . '/'. $files . '">' . $files. '</a></li>'; | |
} | |
} | |
} | |
echo '</ul>'; | |
//If submit button is pressed, copy or move file. | |
//Reset session 'editor_activate' variable. | |
//Refresh when done. | |
if (isset($_POST['copy_move_file'])) | |
{ | |
//If copy radio button is selected, copy file. | |
if ($_POST['file_copy_move'] == 'copy') | |
{ | |
copy($get_file, (getcwd() . '/' . $file_name_only)); | |
} | |
//If move radio button is selected, copy file and delete original. | |
elseif ($_POST['file_copy_move'] == 'move') | |
{ | |
copy($get_file, (getcwd() . '/' . $file_name_only)); | |
delete_file($get_file); | |
} | |
$_SESSION['editor_activate'] = 0; | |
refresh(); | |
} | |
} | |
//Start a PHP session. This is necessary for session variables to be set. | |
//I will have the session be set by a login page when other elements of my CMS backend are complete. | |
if (session_status() == PHP_SESSION_NONE) | |
{ | |
session_start(); | |
//Define server root directory constant. | |
define('ROOT_DIRECTORY', '/var/www'); | |
} | |
//Call different functions based on the session 'editor_activate' variable. | |
switch($_SESSION['editor_activate']) | |
{ | |
//If session 'editor_activate' variable is one, call the editor function. | |
case 1: | |
editor($_SESSION['working_selected_file']); | |
break; | |
//If session 'editor_activate' variable is two, call the rename file and change directory function. | |
case 2: | |
change_directory(); | |
rename_file($_SESSION['working_selected_file']); | |
break; | |
//if session 'editor_activate' variable is three, call the upload file and change directory function. | |
case 3: | |
change_directory(); | |
upload_file(); | |
break; | |
//if session 'editor_activate' variable is four, call the new file and change directory function. | |
case 4: | |
change_directory(); | |
new_file(); | |
break; | |
//if session 'editor_activate' variable, call copy move file and change directory function. | |
case 5: | |
change_directory(); | |
copy_move_file($_SESSION['working_selected_file']); | |
break; | |
//By defauly, call the change directory, display current directory, and call the list directory function. | |
default: | |
change_directory(); | |
echo '<br>'; | |
echo '<h3>Current Directory: ' . getcwd() . '</h3>'; | |
echo '<br>'; | |
list_directory(); | |
break; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment