alert('错误: 无法保存文件。');"; } break; case 'delete': deleteItem($current_path . $_POST['path']); break; case 'chmod': chmodItem($current_path . $_POST['path'], $_POST['permissions']); break; case 'create_folder': $new_folder_name = $_POST['new_folder_name']; $new_folder_path = $current_path . '/' . $new_folder_name; if (!file_exists($new_folder_path)) { mkdir($new_folder_path); } break; case 'create_file': $new_file_name = $_POST['new_file_name']; $new_file_path = $current_path . '/' . $new_file_name; if (!file_exists($new_file_path)) { file_put_contents($new_file_path, ''); } break; case 'delete_selected': if (isset($_POST['selected_paths']) && is_array($_POST['selected_paths'])) { foreach ($_POST['selected_paths'] as $path) { deleteItem($current_path . $path); } } break; } } elseif (isset($_FILES['upload'])) { uploadFile($current_path); } } function deleteItem($path) { $path = rtrim(str_replace('//', '/', $path), '/'); if (!file_exists($path)) { error_log("Attempted to delete non-existent item: $path"); return false; } if (is_dir($path)) { return deleteDirectory($path); } else { if (@unlink($path)) { return true; } else { error_log("Failed to delete file: $path"); return false; } } } function deleteDirectory($dir) { if (!is_dir($dir)) { return false; } $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { $path = $dir . '/' . $file; is_dir($path) ? deleteDirectory($path) : @unlink($path); } return @rmdir($dir); } function readFileWithEncoding($path) { $content = file_get_contents($path); $encoding = mb_detect_encoding($content, ['UTF-8', 'ASCII', 'ISO-8859-1', 'Windows-1252', 'GBK', 'Big5', 'Shift_JIS', 'EUC-KR'], true); return json_encode([ 'content' => mb_convert_encoding($content, 'UTF-8', $encoding), 'encoding' => $encoding ]); } function renameItem($old_path, $new_path) { $old_path = rtrim(str_replace('//', '/', $old_path), '/'); $new_path = rtrim(str_replace('//', '/', $new_path), '/'); $new_name = basename($new_path); $dir = dirname($old_path); $new_full_path = $dir . '/' . $new_name; if (!file_exists($old_path)) { error_log("Source file does not exist before rename: $old_path"); if (file_exists($new_full_path)) { error_log("But new file already exists: $new_full_path. Rename might have succeeded."); return true; } return false; } $result = rename($old_path, $new_full_path); if (!$result) { error_log("Rename function returned false for: $old_path to $new_full_path"); if (file_exists($new_full_path) && !file_exists($old_path)) { error_log("However, new file exists and old file doesn't. Consider rename successful."); return true; } } if (file_exists($new_full_path)) { error_log("New file exists after rename: $new_full_path"); } else { error_log("New file does not exist after rename attempt: $new_full_path"); } if (file_exists($old_path)) { error_log("Old file still exists after rename attempt: $old_path"); } else { error_log("Old file no longer exists after rename attempt: $old_path"); } return $result; } function editFile($path, $content, $encoding) { if (file_exists($path) && is_writable($path)) { return file_put_contents($path, $content) !== false; } return false; } function chmodItem($path, $permissions) { chmod($path, octdec($permissions)); } function uploadFile($destination) { $uploaded_files = []; $errors = []; foreach ($_FILES["upload"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["upload"]["tmp_name"][$key]; $name = basename($_FILES["upload"]["name"][$key]); $target_file = rtrim($destination, '/') . '/' . $name; if (move_uploaded_file($tmp_name, $target_file)) { $uploaded_files[] = $name; } else { $errors[] = "上传 $name 失败"; } } else { $errors[] = "文件 $key 上传错误: " . $error; } } $result = []; if (!empty($errors)) { $result['error'] = implode("\n", $errors); } if (!empty($uploaded_files)) { $result['success'] = implode(", ", $uploaded_files); } return $result; } if (!function_exists('deleteDirectory')) { function deleteDirectory($dir) { if (!file_exists($dir)) return true; if (!is_dir($dir)) return unlink($dir); foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) return false; } return rmdir($dir); } } 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('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); exit; } } function getDirectoryContents($dir) { $contents = array(); foreach (scandir($dir) as $item) { if ($item != "." && $item != "..") { $path = $dir . DIRECTORY_SEPARATOR . $item; $perms = '----'; $size = '-'; $mtime = '-'; $owner = '-'; if (file_exists($path) && is_readable($path)) { $perms = substr(sprintf('%o', fileperms($path)), -4); if (!is_dir($path)) { $size = formatSize(filesize($path)); } $mtime = date("Y-m-d H:i:s", filemtime($path)); $owner = function_exists('posix_getpwuid') ? posix_getpwuid(fileowner($path))['name'] : fileowner($path); } $contents[] = array( 'name' => $item, 'path' => str_replace($dir, '', $path), 'is_dir' => is_dir($path), 'permissions' => $perms, 'size' => $size, 'mtime' => $mtime, 'owner' => $owner, 'extension' => pathinfo($path, PATHINFO_EXTENSION) ); } } return $contents; } function formatSize($bytes) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= (1 << (10 * $pow)); return round($bytes, 2) . ' ' . $units[$pow]; } $contents = getDirectoryContents($current_path); $breadcrumbs = array(); $path_parts = explode('/', trim($current_dir, '/')); $cumulative_path = ''; foreach ($path_parts as $part) { $cumulative_path .= $part . '/'; $breadcrumbs[] = array('name' => $part, 'path' => $cumulative_path); } if (isset($_GET['action']) && $_GET['action'] === 'search' && isset($_GET['term'])) { $searchTerm = $_GET['term']; $searchResults = searchFiles($current_path, $searchTerm); echo json_encode($searchResults); exit; } function searchFiles($dir, $term) { $results = array(); $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST ); $webRoot = $_SERVER['DOCUMENT_ROOT']; $tmpDir = sys_get_temp_dir(); foreach ($files as $file) { if ($file->isDir()) continue; if (stripos($file->getFilename(), $term) !== false) { $fullPath = $file->getPathname(); if (strpos($fullPath, $webRoot) === 0) { $relativePath = substr($fullPath, strlen($webRoot)); } elseif (strpos($fullPath, $tmpDir) === 0) { $relativePath = 'tmp' . substr($fullPath, strlen($tmpDir)); } else { $relativePath = $fullPath; } $relativePath = ltrim($relativePath, '/'); $results[] = array( 'path' => $relativePath, 'dir' => dirname($relativePath), 'name' => $file->getFilename() ); } } return $results; } ?> NeKobox文件助手
Home Mihomo Sing-box Convert File Assistant
Neko Box

NeKoBox File Assistant

Name Type Size Modified Time Permissions Owner Actions
.. Directory - - - -
Line: 1, Column: 1 Character Count: 0