Reputation: 836
I'm trying to display the contents of a folder on my local HDD as links in a web browser. This is how I get the contents of a folder
$dir = scandir($path);
foreach($dir as $token)
{
if(($token != ".") && ($token != ".."))
{
if(is_dir($path.'/'.$token))
{
$folders[] = $token;
}
else
{
$files[] = $token;
}
}
}
foreach($folders as $folder)
{
$newpath = $path.'/'.$folder;
echo "<a href = tema2.php?cale=$newpath> [ $folder ] </a>" . "<br>";
}
foreach($files as $file)
{
$newpath = $path.'/'.$file;
echo "<a href = file:///$newpath> $file </a>" . "<br>";
}
Everything works fine except the links to the files which do nothing when pressed. The links that show up in my web browser are like this : "file:///C:/folder/test.txt". Tried this is Firefox, Chrome and IE.
Upvotes: 2
Views: 11106
Reputation: 1
I circumvent this by creating symbolic links in the apache document folder to any folders containing content I wish to share. So say I want to serve up files from "Z:/Media/", I create a symbolic link named "Media" within "D:/Server/HTTP/" (my Apache document root folder) and then I can serve a file from there such as "/Media/Movies/Fargo.mp4".
Upvotes: 0
Reputation: 9583
If the file is outside of the scope of the web server's folder it will not be able to access the file to deliver it.
You can either create a file handler to deliver the files:
so change echo "<a href = file:///$newpath> $file </a>" . "<br>";
to echo "<a href = \"fileHandler.php?file=$file\"" . "<br>";
and create fileHandler.php as:
<?php
$file = $_GET['file'];
header('content-type:application/'.end(explode('.',$file)));
Header("Content-Disposition: attachment; filename=" . $file); //to set download filename
exit(file_get_contents($file));
?>
or bypass the web server and link to file directly (this will only work over LAN or VPN)
so change echo "<a href = file:///$newpath> $file </a>" . "<br>";
to echo "<a href ='$_SERVER[HTTP_HOST]/$newpath/$file'>$file</a><br />"
Upvotes: 4
Reputation: 9913
This will not work
you cant link a file outside of Apache's htdocs folder
2 options :
1) move the files you need to htdocs
2) use virtual hosts / DocumentRoot
http://httpd.apache.org/docs/current/en/urlmapping.html
Make XAMPP/Apache serve file outside of htdocs
Upvotes: 1