douzhuanqian8244 2013-05-14 18:50 采纳率: 100%
浏览 24

使用PHP和htaccess在XAMPP中几乎浏览Windows文件系统

I'm developing an app to enable browsing the Windows file system in a web browser. This was originally conceived of as a solution for allowing my wife to look at photos stored on an external hard drive connected to a network computer running XAMPP, thus alleviating the need to install any software. I've gone on to allow viewing movies, browsing music, etc. using various techniques to serve the content. Woohoo!

There are no problems accessing the file system using the following in httpd.conf (Apache Config):

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

Originally, I was creating a folder parameter using a query string in the URL:

index.php?q1=Pictures

Then creating a $path variable:

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

Then feeding the $path variable into scan_dir:

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

Then iterating through the results, changing the $file_or_folder variable with some additional logic (not included) to a file link or a folder link:

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>
}

It works well, but the query string was getting pretty ugly when a file or folder was a few levels deep, eg:

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

and the code required to parse the file path was even worse:

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";
}

Instead of getting the file path that way, I figured using .htaccess to redirect the requests was more elegant. So, I revised the code so that "/level_A/level_B/level_C" is redirected to index.php and the path info to feed into scan_dir() is created this way:

#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();

So, the folder links now look like this:

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

This works great, but only two levels deep. At the third level, the first folder is repeated. In other words, a link such as "level_A/level_B/level_C/" is being sent to the browser as "level_A/level_A/level_B/level_C/". I'm missing something, probably simple, but I can't find it.

Thanks in advance to anyone willing to take a crack at my convoluted question.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据