'error', 'message' => 'An unknown error occurred.']; $action = $_POST['action'] ?? $_GET['action'] ?? 'list'; $current_path = getPath(); try { switch ($action) { case 'list': $items = []; if (is_dir($current_path) && $handle = opendir($current_path)) { while (false !== ($entry = readdir($handle))) { if ($entry == "." || $entry == "..") continue; $full_path = $current_path . DIRECTORY_SEPARATOR . $entry; $is_dir = is_dir($full_path); // --- BADLAAV ---: Directory ke liye item count aur file ke liye size calculate karna $size_or_count = '-'; if ($is_dir) { $count = get_dir_item_count($full_path); if ($count === 0) { $size_or_count = 'Empty'; } else { $size_or_count = $count . ' item' . ($count > 1 ? 's' : ''); } } else { $size_or_count = formatBytes(filesize($full_path)); } $items[] = [ 'name' => $entry, 'is_dir' => $is_dir, 'size' => $size_or_count, // Ab isme count ya size dono ho sakta hai 'modified' => date("Y-m-d H:i:s", filemtime($full_path)), 'path' => $full_path, 'ext' => $is_dir ? '' : strtolower(pathinfo($entry, PATHINFO_EXTENSION)) ]; } closedir($handle); } usort($items, function ($a, $b) { if ($a['is_dir'] == $b['is_dir']) return strnatcasecmp($a['name'], $b['name']); return $a['is_dir'] ? -1 : 1; }); $breadcrumbs = []; $path_so_far_real = $script_root; $breadcrumbs[] = ['name' => 'Home', 'path' => $script_root]; $path_parts = explode(DIRECTORY_SEPARATOR, substr($current_path, strlen($script_root))); foreach ($path_parts as $part) { if (empty($part)) continue; $path_so_far_real .= DIRECTORY_SEPARATOR . $part; $breadcrumbs[] = ['name' => $part, 'path' => $path_so_far_real]; } $response = [ 'status' => 'success', 'data' => [ 'items' => $items, 'breadcrumbs' => $breadcrumbs, 'path' => $current_path, 'editable_extensions' => $editable_extensions, 'viewable_extensions' => $viewable_extensions_direct_output, 'document_root' => realpath($_SERVER['DOCUMENT_ROOT']) ] ]; break; // ... (baaki sabhi cases 'delete', 'rename', etc. waise hi rahenge) ... case 'delete': $item_to_delete = $current_path . DIRECTORY_SEPARATOR . basename($_POST['item']); if (file_exists($item_to_delete) && strpos(realpath($item_to_delete), $script_root) === 0) { // Security check if (is_dir($item_to_delete)) { $it = new RecursiveDirectoryIterator($item_to_delete, RecursiveDirectoryIterator::SKIP_DOTS); $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); foreach($files as $file) { if ($file->isDir()){ rmdir($file->getRealPath()); } else { unlink($file->getRealPath()); } } rmdir($item_to_delete); } else { unlink($item_to_delete); } $response = ['status' => 'success', 'message' => '"' . htmlspecialchars($_POST['item']) . '" was deleted.']; } else { $response['message'] = 'Item not found or permission denied.'; } break; case 'rename': $old_name = $current_path . DIRECTORY_SEPARATOR . basename($_POST['old_name']); $new_name = $current_path . DIRECTORY_SEPARATOR . basename($_POST['new_name']); if(file_exists($old_name) && !file_exists($new_name) && strpos(realpath($old_name), $script_root) === 0) { // Security if (rename($old_name, $new_name)) { $response = ['status' => 'success', 'message' => 'Renamed successfully.']; } else { $response['message'] = 'Could not rename.'; } } else { $response['message'] = 'Old name not found or new name already exists.'; } break; case 'create_folder': $new_folder = $current_path . DIRECTORY_SEPARATOR . basename($_POST['name']); if(!file_exists($new_folder)) { if(mkdir($new_folder, 0755)) { $response = ['status' => 'success', 'message' => 'Folder created.']; } else { $response['message'] = 'Could not create folder.'; } } else { $response['message'] = 'Folder already exists.'; } break; case 'create_file': $new_file = $current_path . DIRECTORY_SEPARATOR . basename($_POST['name']); if(!file_exists($new_file)) { if(file_put_contents($new_file, '') !== false) { $response = ['status' => 'success', 'message' => 'File created.']; } else { $response['message'] = 'Could not create file.'; } } else { $response['message'] = 'File already exists.'; } break; case 'upload': if (isset($_FILES['files'])) { $file = $_FILES['files']; $target_file = $current_path . DIRECTORY_SEPARATOR . basename($file['name']); if (move_uploaded_file($file['tmp_name'], $target_file)) { $response = ['status' => 'success', 'message' => 'File uploaded.']; } else { $response['message'] = 'Upload failed.'; } } else { $response['message'] = 'No file received.'; } break; case 'get_content': $file_path = $current_path . DIRECTORY_SEPARATOR . basename($_GET['file']); $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION)); // Handle extensionless files if (strpos(basename($file_path), '.') === false) $ext = ''; if (file_exists($file_path) && in_array($ext, $editable_extensions)) { $response = [ 'status' => 'success', 'data' => [ 'content' => file_get_contents($file_path), 'filename' => basename($file_path) ] ]; } else { $response['message'] = 'Cannot read file or file is not editable.'; } break; case 'save_content': $file_path = $current_path . DIRECTORY_SEPARATOR . basename($_POST['file']); $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION)); // Handle extensionless files if (strpos(basename($file_path), '.') === false) $ext = ''; if (file_exists($file_path) && in_array($ext, $editable_extensions)) { if (file_put_contents($file_path, $_POST['content']) !== false) { $response = ['status' => 'success', 'message' => 'File saved successfully.']; } else { $response['message'] = 'Failed to save file.'; } } else { $response['message'] = 'Cannot save file or file is not editable.'; } break; } } catch (Exception $e) { $response = ['status' => 'error', 'message' => $e->getMessage()]; } echo json_encode($response); exit;}?> AJAX File Manager

File Manager

Naam Size / Items Badla hua