2

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.

Adrian Buzea
  • 826
  • 2
  • 11
  • 33
  • possible duplicate of [Open a direct file on the hard drive from firefox (file:///)](http://stackoverflow.com/questions/2148584/open-a-direct-file-on-the-hard-drive-from-firefox-file) – makallio85 Mar 23 '14 at 15:36

3 Answers3

4

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 />"

hichris123
  • 10,145
  • 15
  • 56
  • 70
andrew
  • 9,313
  • 7
  • 30
  • 61
  • I tried the filehandler and it doesn't work. If what you say is true then why would it work because filehandler.php still wouldn't have access outside the web server folder . – Adrian Buzea Mar 23 '14 at 15:49
  • apache can't move above its root as defined in the apache configuration file (usually /var/www/sites-available on *nix systems) but the php can (provided it has permission to access the folder), that's the beauty of a file handler, it allows you to keep your private and public files separate while still being able to deliver certain private files – andrew Mar 23 '14 at 15:55
  • Actually it works, but in a weird way. When I click on a file it prompts me to download filehandler.php which has the contents of the file inside. – Adrian Buzea Mar 23 '14 at 15:57
  • that is what the line `header('content-type:application/'.end(explode('.',$file)));` is for, its an instruction to tell php to serve up the file, rather than displaying its contents in the browser – andrew Mar 23 '14 at 16:01
  • well yeah but i don't want to download a php file and then open that... i understand what the code is trying to do but i'm not sure i'm getting the desired result – Adrian Buzea Mar 23 '14 at 16:03
  • sorry, yes I see what you mean, you can change the name of the output file name, I'll add it in the edit – andrew Mar 23 '14 at 16:03
  • I now see that it adds some weird html stuff to the beginning of the document ( http://pastebin.com/sKyh4Q1n ). Any idea why that is ? – Adrian Buzea Mar 23 '14 at 16:12
  • I don't know I'd need to see what is at line 4 in fileHandler.php, you should probably post it as a separate question – andrew Mar 23 '14 at 19:05
1

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

Community
  • 1
  • 1
Nimrod007
  • 9,825
  • 8
  • 48
  • 71
0

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".