如何在文件管理器脚本中设置自定义路径以浏览查看内容?
我希望在这份文件管理器脚本代码中设置自定义或可选路径/目录来查看其中的文件夹与文件,请问该如何实现?相关代码如下:
function get_absolute_path($path) { $path = str_replace(['/', '\'], DIRECTORY_SEPARATOR, $path); $parts = explode(DIRECTORY_SEPARATOR, $path); $absolutes = []; foreach ($parts as $part) { if ('.' == $part) continue; if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } return implode(DIRECTORY_SEPARATOR, $absolutes); }
Hey there! Your get_absolute_path function is already solid at resolving relative paths to absolute ones—perfect foundation for adding custom path support. Here are two straightforward, practical ways to implement optional directory viewing in your file manager:
1. Modify the Path Function to Support Optional Base & Target Paths
First, update your existing function to accept optional parameters, so it can handle both relative and custom absolute paths seamlessly:
function get_absolute_path($path = '.', $basePath = null) { // Use current working directory as default base if none is provided if ($basePath === null) { $basePath = getcwd(); } // Combine base path with input path to handle relative targets $fullPath = $basePath . DIRECTORY_SEPARATOR . $path; $fullPath = str_replace(['/', '\'], DIRECTORY_SEPARATOR, $fullPath); $parts = explode(DIRECTORY_SEPARATOR, $fullPath); $absolutes = []; foreach ($parts as $part) { if (empty($part) || $part === '.') { continue; } if ($part === '..') { array_pop($absolutes); } else { $absolutes[] = $part; } } return implode(DIRECTORY_SEPARATOR, $absolutes); }
Usage Examples:
- View current directory:
get_absolute_path() - View a relative custom path:
get_absolute_path('docs/2024_reports') - View a specific absolute path directly:
get_absolute_path('', '/var/www/html/public')
2. Add a Dedicated Function to List Directory Contents
Since your path resolver is now flexible, create a helper function to actually fetch and return files/folders from a target path. This keeps your code clean and focused:
function list_directory_contents($targetPath = '.') { // Resolve the target path to a valid absolute path $absolutePath = get_absolute_path($targetPath); // Validate the path exists and is accessible if (!is_dir($absolutePath)) { return "Error: Directory '$absolutePath' does not exist or is not accessible."; } // Get all contents, then filter out the default '.' and '..' entries $contents = scandir($absolutePath); $filteredContents = array_filter($contents, function($item) { return $item !== '.' && $item !== '..'; }); return $filteredContents; }
Usage Examples:
- List current directory contents:
print_r(list_directory_contents()); - List a custom relative directory:
print_r(list_directory_contents('downloads')); - List an absolute directory:
print_r(list_directory_contents('/home/user/personal_notes'));
Quick Security & Reliability Tip
If this is for a web-based file manager, make sure to add extra checks to restrict access to allowed directories only—this prevents malicious path traversal attacks (e.g., blocking attempts to access /etc/passwd or other system files outside your intended root folder).
内容的提问来源于stack exchange,提问作者Samir Z. Mohammed

