Opening File over UNC path

rcs2008
rcs2008 used Ask the Experts™
on
I am trying to use php to open a PDF file.  I have been able to access the share and I know permissions are working just wondering what might be going wrong.  Below is the code and also a screen shot of what i see
$file = '//revere-acct/start$/NOT_PROCESSED/400000104_PR_D32555_20100121_000.PDF';
	$Out.= "<br /><iframe src='".$file."' style=\"width:750px; height:1000px;\" frameborder=\"0\"></iframe>";

Open in new window

RCS-MyPanel---Employee-126463501.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
Based on your code the computer that Firefox is running on must have access to the computer where the share is.

When you accessed the share were you doing it from the computer where firefox is or on the computer where the PHP code is running.

I am assuming that the PHP code is running on a Web server that is on a different computer from the browser.

Author

Commented:
Yes the share is on "Computer A" webserver is on "Computer B" and I am accessing from "Computer C"
Richard QuadlingSenior Software Developer

Commented:
You are trying to use the FILE protocol and not the HTTP protocol.

So, try file:/// (yes 3 slashes) and then the UNC path.

BUT!!!!!

I think FireFox blocks links to local files (local as in file:/// protocol).
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Author

Commented:
Any idea how I could make the webserver push it to me?
Senior Software Developer
Commented:
Remember that pushing the file will only get the end user a copy of the file not access to the original....

But ....

what would happen if you pushed a shortcut to the client ...

Hmm ...

Never done that.

http://www.optimumx.com/download/#Shortcut has a program to allow you to dynamically create shortcuts.

I use this to create shortcuts for a new user's desktop.

Hmmm ... thinking .... quite interesting.

But, pushing a file. This is some edited code from one of my classes.

Now, if the file pushed was a shortcut with a mime type of application/x-ms-shortcut (http://filext.com/file-extension/LNK) ...

That might allow "local" files to be provided via links.


<?php
// Do your session validation and redirection first.

// You will need to have something like $_GET['FileID'] to determine which file.

// You will need to translate the $_GET['FileID'] to the actual file
name => $PhysicalFileName

// I use the following headers to force a downloads.

// You can change the Content-Type to application/msexcel of Excel or
application/pdf for Adobe Acrobat files, etc.

header('HTTP/1.1 200 OK', True, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename="document.doc"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($PhysicalFileName));

// readfile() will simply pass the file from the drive straight to the client.
readfile($PhysicalFileName);

// Exit to close the process.
exit;

Open in new window

Richard QuadlingSenior Software Developer

Commented:
The readfile() function is the important bit. The headers there were to force a save dialogue.

Author

Commented:
I am trying to display the file via UNC and within the users browser, below is an example
RCS-MyPanel---Employee-126469351.png
Richard QuadlingSenior Software Developer

Commented:
It all comes down to the headers. In the end, I ended up forcing downloads as trying to consistently open certain files is a pain.

Author

Commented:
the only type of file I want to display in the browser is a PDF
Richard QuadlingSenior Software Developer

Commented:
Aha.

Can you try ...

<?php
header('HTTP/1.1 200 OK', True, 200);
header('Content-Type: application/pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($PhysicalFileName));
readfile($PhysicalFileName);
exit();
?>
Distinguished Expert 2017

Commented:
If the file is on a share, you might want to stream the data to the browser as opposed to referring the browser to a share which the user might not have access to.
i.e. print "content type: application/pdf"
Print the pdf file to the browser or setup a script on your web server
http:/myserver/document.php?documentid=32343
The script based on the documentid will open the applicable file and will stream it.
Top Expert 2009

Commented:
I'm using the code below without any issues to stream files from a windows share to browsers. You only have to be sure that the user the webserver is running under has access to the drive and file.
$file='\\\\revere-acct\\start$\\NOT_PROCESSED\\400000104_PR_D32555_20100121_000.PDF';
if(file_exists($file)) {
	$fh=fopen($file, "rb");
	header("Content-Type: application/pdf");
	header("Content-Length: " . filesize($file));
	header("Content-Disposition: attachment; filename=\"400000104_PR_D32555_20100121_000.PDF\"");
	fpassthru($fh);
} else {
    echo "Could not find this file";
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial