0

我正在开发一个应用程序,以便在 Web 浏览器中浏览 Windows 文件系统。这最初被设想为一种解决方案,允许我的妻子查看存储在连接到运行 XAMPP 的网络计算机的外部硬盘驱动器上的照片,从而减轻安装任何软件的需要。我继续允许使用各种技术提供内容来观看电影、浏览音乐等。呜呼!

使用 httpd.conf (Apache Config) 中的以下内容访问文件系统没有问题:

<Directory "H:/">
Require all granted
Allow from all
RewriteEngine On
RewriteOptions Inherit
</Directory>
Alias /ExternalDrive "H:/"

最初,我使用 URL 中的查询字符串创建文件夹参数:

index.php?q1=Pictures

然后创建一个 $path 变量:

if (isset($_GET['q1'])) {
$q1=($_GET['q1']);
$path=$q1."/";
}

然后将 $path 变量输入 scan_dir:

$scan_dir="H:/$path";
$files_or_folders = scandir($scan_dir); 

然后遍历结果,将带有一些附加逻辑(不包括在内)的 $file_or_folder 变量更改为文件链接或文件夹链接:

foreach($files_or_folders as $key => $file_or_folder){
// if files
// display pictures or other content
<a href="<?= $file; ?>"><?= $file ?></a>
// if folders
// display a link to the folder
<a href="index.php<?= $query."=".$folder; ?>"><?= $folder ?></a>
}

它运行良好,但是当文件或文件夹深几级时,查询字符串变得非常难看,例如:

index.php?q1=level_A&q2=level_B&q3=level_C

解析文件路径所需的代码更糟糕:

if (isset($_GET['q1']) && isset($_GET['q2']) && !isset($_GET['q3'])){
$q1=($_GET['q1']);
$q2=($_GET['q2']);
    $q3=($_GET['q3']);
$path=$q1."/".$q2."/".$q3;
$query="?q1=$q1&q2=$q2&q3";
}

我没有以这种方式获取文件路径,而是认为使用 .htaccess 重定向请求更优雅。因此,我修改了代码,以便将“/level_A/level_B/level_C”重定向到 index.php,并以这种方式创建输入 scan_dir() 的路径信息:

#get uri (everything after www root)
$request = $_SERVER['REQUEST_URI'];

#explode the path by '/'   
$folders  = explode("/", $request); 

ob_start();
#loop through the results
foreach ($folders as $folder) {
echo $folder;
echo "/";
}
#save ob results in variable
$path = ob_get_clean();

因此,文件夹链接现在如下所示:

<a href="<?= $path.$folder ?>">$folder/a>

这很好用,但只有两个层次。在第三级,重复第一个文件夹。换句话说,诸如“level_A/level_B/level_C/”之类的链接作为“level_A/level_A/level_B/level_C/”被发送到浏览器。我错过了一些东西,可能很简单,但我找不到。

在此先感谢任何愿意解答我复杂问题的人。

4

0 回答 0