PHP scandir explorer view to display network share

2019-09-06 20:18发布

问题:

Maybe I am being daft, or misunderstanding either WAMP restrictions or browser restrictions, but I have created a PHP file explorer view using the scandir function recursively using Ajax, and it works great to display the files from our network share (\computername\share).

I can launch the files when accessing http://localhost, however if from the same localhost machine I access http://[our_external_ip_address] then the files do not open. The path in the status bar displays the same on both (e.g. file://computername/share/filename.zip). I eventually want to put the files on a network share which is on the same domain as our web host machine.

Please help. I am fairly competent with PHP & JS but when it comes to web hosts and the like, I am stuffed. TIA.

James

回答1:

Massive thanks to DaveRandom - this solution worked to allow PDF / Word / Zip files from a UNC path to be launched from an external web client.

FILE 1: - index.php

<?php
    $filename= *** insert UNC path to file *** // e.g. \\share\computername\New Document.doc
    $extension= pathinfo($filename, PATHINFO_EXTENSION); // Gets extension for file
    $displayFilename= *** insert shorten filename *** // e.g. New Document.doc
?>


<a href="filehandler.php?name=<?php echo $filename; ?>&ext=<?php echo $extension; ?>&shortname=<?php echo $displayFilename; ?>"><?php echo $displayFilename; ?></a>

FILE 2: - filehandler.php

<?php
$filename = $_GET['name'];
$extension = $_GET['ext'];
$shortfilename = $_GET['shortname'];
if ($extension == "pdf")
{
    header("Content-type: application/pdf"); // act as a pdf file to browser
}
if ($extension == "doc")
{
    header("Content-type: application/msword"); // act as a doc file to browser
}
if ($extension == "zip")
{
    header("Content-type: application/zip"); // act as a zip file to browser
}
header("Content-Disposition: inline; filename='$shortfilename'"); 

$file = readfile($filename);
echo $file;
?>