我正在嘗試創建一個檔案服務器。
我想要做的是 -> 在頁面上會顯示很多檔案夾,當你點擊一個檔案夾時,你會看到它的內容,如果這個檔案夾中有一個檔案夾,你可以點擊它,它會顯示該檔案夾的內容。
這是我目前擁有的代碼,但是我不知道如何進一步進行,因為我對 PHP 很陌生。
function searchFolders($dir){
$filesInFolder = array();
$iterator = new FilesystemIterator($dir);
foreach($iterator as $file){
if(is_dir($file)){
echo "<h1>Folder</h1>";
searchFolders($file);
}else{
$filesInFolder[] = $file;
}
}
foreach($filesInFolder as $file){
echo "<a href='/$file'> $file </a><br>";
}
}
searchFolders('src')
uj5u.com熱心網友回復:
下面是我相信會幫助你的東西。我已經使用 PHP 的
此示例使用兩個檔案,Reader.php并且index.php...
閱讀器.php
<?php
class Reader {
public function __construct(
public string $root
) {}
/**
* Remove the root directory from the path value.
*
* @param string $path
* @return string
*/
public function removeRootFromPath(string $path) {
$path = preg_replace('/^' . preg_quote($this->root, '/') . '/', '', $path);
$path = $this->cleanPath($path);
return DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
}
/**
* Add the root directory to the path value.
*
* @param string $path
* @return string
*/
public function addRootToPath(string $path) {
$path = $this->removeRootFromPath($path);
$path = ltrim($path, DIRECTORY_SEPARATOR);
$root = rtrim($this->root, DIRECTORY_SEPARATOR);
return $root . DIRECTORY_SEPARATOR . $path;
}
/**
* Replace dot notation in paths i.e ../ and ./
*
* @param string $dir
* @return string
*/
public function cleanPath(string $dir) {
$sep = preg_quote(DIRECTORY_SEPARATOR, '/');
return preg_replace('/\.\.' . $sep . '|\.' . $sep . '/', '', $dir);
}
/**
* @param string $dir
* @return FilesystemIterator|null
*/
public function readDirectory(string $dir) {
$dir = $this->addRootToPath($dir);
try {
return new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
} catch (UnexpectedValueException $exception) {
return null;
}
}
}
索引.php
<?php
require_once 'Reader.php';
$root_dir = 'src'; // Relative to current file. Change to your path!
$reader = new Reader(__DIR__ . DIRECTORY_SEPARATOR . $root_dir);
$target = $reader->removeRootFromPath(!empty($_GET['path']) ? $_GET['path'] : '/');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Directory viewer</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container my-5">
<h1>Current: <?= $target; ?></h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th class="w-25">
Type
</th>
<th class="w-75">
File
</th>
</tr>
</thead>
<tbody>
<?php if ($target !== $reader->removeRootFromPath('/')): ?>
<tr>
<td>
Parent
</td>
<td>
<a href="?path=<?= urlencode($reader->removeRootFromPath(dirname($target))); ?>">../</a>
</td>
</tr>
<?php endif ?>
<?php if ($results = $reader->readDirectory($target)): ?>
<?php foreach($results as $result): ?>
<?php
// Make the full path user friendly by removing the root directory.
$user_friendly = $reader->removeRootFromPath($result->getFileInfo());
$type = $result->getType();
?>
<tr>
<td>
<?= ucfirst($type); ?>
</td>
<td>
<?php if ($type === 'dir'): ?>
<a href="?path=<?= urlencode($user_friendly); ?>"><?= $user_friendly; ?></a>
<?php else: ?>
<!-- Maybe add a download link to the file -->
<?= $user_friendly; ?>
<?php endif ?>
</td>
</tr>
<?php endforeach ?>
<?php else: ?>
<tr>
<td colspan="2">
Directory does not exist.
</td>
</tr>
<?php endif ?>
</tbody>
</table>
</div>
</body>
</html>
Reader類包括一些輔助方法removeRootFromPath(),addRootToPath()以及cleanPath()這些組合在那里幫助阻止訪問源目錄的路徑以外的用戶。
例如,如果您的檔案系統看起來像......
/var/www/Reader.php
/var/www/index.php
/var/www/src/
而且您列出了/var/www/src/目錄,沒有路徑清理方法,用戶可以修改輸入../../并最終到達您的檔案系統根目錄,這是您不想要的!
這是我為了這個問題而快速整理的東西,應該用作指南。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367716.html
