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.