Link to file on local hard drive

Adrian Buzea

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.

andrew

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related