Bluehost.com Web Hosting $6.95

Displaying files

This is a discussion on Displaying files within the PHP General forums, part of the PHP Programming Forums category; I think this should be a simple question and answer, but after looking through the manual and goggling I have ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-14-2008
Dan Shirah
 
Posts: n/a
Default Displaying files

I think this should be a simple question and answer, but after looking
through the manual and goggling I have yet to find an answer.

Question: Using PHP, how would I go about opening a file in a window, table
or div?

Example: I have a file called 123.xls. When a condition is met, I want PHP
to open this file into the current window.

I know I can use Javascript to easily do this, but due to the requirements
of my application, javascript is not an option here.

I tried using fopen() but that only opens the file in the background for PHP
to read/write to and will not display the file in a browser.

Any ideas?

Thx,
Dan

Reply With Quote
  #2 (permalink)  
Old 08-14-2008
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Displaying files

Dan Shirah wrote:
> I think this should be a simple question and answer, but after looking
> through the manual and goggling I have yet to find an answer.
>
> Question: Using PHP, how would I go about opening a file in a window, table
> or div?
>
> Example: I have a file called 123.xls. When a condition is met, I want PHP
> to open this file into the current window.
>
> I know I can use Javascript to easily do this, but due to the requirements
> of my application, javascript is not an option here.
>
> I tried using fopen() but that only opens the file in the background for PHP
> to read/write to and will not display the file in a browser.
>
> Any ideas?
>
> Thx,
> Dan
>


Who needs PHP... :)

The following will open a new browser window and load the href in it.

<a href="http://examples.com/123.xls" target="_blank">Open It!!!</a>

If that isn't what you are looking for, please give a little more details.

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

Reply With Quote
  #3 (permalink)  
Old 08-14-2008
Dan Shirah
 
Posts: n/a
Default Re: [PHP] Displaying files

>
> Who needs PHP... :)
>>
>> The following will open a new browser window and load the href in it.
>>
>> <a href="http://examples.com/123.xls" target="_blank">Open It!!!</a>
>>
>> If that isn't what you are looking for, please give a little more details.

>
>

You're killin' me, Jim! :)

I need to open the file from the server, using PHP. I already know how to
open it using HTML and Javascript, but those methods cannot be used for this
section of my application.

I need to call and open the document in the browser window using PHP.

Example: If I was going to do it with Javascript I would just use something
simple like this;

function open() {
window.open('open.php');
}

Or, if I was going to use HTML I would use something simlar to what you
posted.

How can I achieve that same functionality using only PHP? Or is it not
possible?

Reply With Quote
  #4 (permalink)  
Old 08-14-2008
Stut
 
Posts: n/a
Default Re: [PHP] Displaying files

On 14 Aug 2008, at 21:06, Dan Shirah wrote:
>>
>> Who needs PHP... :)
>>>
>>> The following will open a new browser window and load the href in
>>> it.
>>>
>>> <a href="http://examples.com/123.xls" target="_blank">Open It!!!</a>
>>>
>>> If that isn't what you are looking for, please give a little more
>>> details.

>>
>>

> You're killin' me, Jim! :)
>
> I need to open the file from the server, using PHP. I already know
> how to
> open it using HTML and Javascript, but those methods cannot be used
> for this
> section of my application.
>
> I need to call and open the document in the browser window using PHP.
>
> Example: If I was going to do it with Javascript I would just use
> something
> simple like this;
>
> function open() {
> window.open('open.php');
> }
>
> Or, if I was going to use HTML I would use something simlar to what
> you
> posted.
>
> How can I achieve that same functionality using only PHP? Or is it
> not
> possible?


You need to know the mime type for the file you're serving. Call
header('Content-Type: x/y'); where x/y is the mime type. Then call
readfile('/path/to/file/on/server'); to output the file to the browser.

-Stut

--
http://stut.net/
Reply With Quote
  #5 (permalink)  
Old 08-14-2008
Dan Shirah
 
Posts: n/a
Default Re: [PHP] Displaying files

>
> You need to know the mime type for the file you're serving. Call
>> header('Content-Type: x/y'); where x/y is the mime type. Then call
>> readfile('/path/to/file/on/server'); to output the file to the browser.
>>
>> -Stut

>
>


Stut, trying that method gives me the following: PHP Warning: readfile(
\\server\folder\file.xls) [<a
href='function.readfile'>function.readfile</a>]: failed to open stream:
Invalid argument on line 44

Here's my code. Line 44 is where it stated readfile()
This document is located on a seperate server.


<?php
$folder = $_GET['folder'];
$file = $_GET['file'];

$filename = "\\\\server\\".$folder."\\".$file.".xls";
header("Content-Type: application/x-msdownload");
readfile($filename);
?>

Reply With Quote
  #6 (permalink)  
Old 08-14-2008
Stut
 
Posts: n/a
Default Re: [PHP] Displaying files

On 14 Aug 2008, at 21:29, Dan Shirah wrote:
> You need to know the mime type for the file you're serving. Call
> header('Content-Type: x/y'); where x/y is the mime type. Then call
> readfile('/path/to/file/on/server'); to output the file to the
> browser.
>
> -Stut
>
>
> Stut, trying that method gives me the following: PHP Warning:
> readfile(\\server\folder\file.xls) [<a
> href='function.readfile'>function.readfile</a>]: failed to open
> stream: Invalid argument on line 44
>
> Here's my code. Line 44 is where it stated readfile()
> This document is located on a seperate server.
>
> <?php
> $folder = $_GET['folder'];
> $file = $_GET['file'];
>
> $filename = "\\\\server\\".$folder."\\".$file.".xls";
> header("Content-Type: application/x-msdownload");
> readfile($filename);
> ?>
>


That simply means it can't open the file. Make sure the machine this
is running on has everything it needs to access that UNC filename.

-Stut

--
http://stut.net/
Reply With Quote
  #7 (permalink)  
Old 08-14-2008
Dan Shirah
 
Posts: n/a
Default Re: [PHP] Displaying files

That simply means it can't open the file. Make sure the machine this is
running on has everything it needs to access that UNC filename.

-Stut


Stut,

If I copy the link from the error message and paste it into a browser
running from my PHP server, the file comes up just fine.

Should I try mkdir() or mkpath() to set the server and folder location and
then try it from there?

Reply With Quote
  #8 (permalink)  
Old 08-14-2008
Stut
 
Posts: n/a
Default Re: [PHP] Displaying files

On 14 Aug 2008, at 21:57, Dan Shirah wrote:
> That simply means it can't open the file. Make sure the machine this
> is running on has everything it needs to access that UNC filename.
>
> -Stut
>
>
> Stut,
>
> If I copy the link from the error message and paste it into a
> browser running from my PHP server, the file comes up just fine.
>
> Should I try mkdir() or mkpath() to set the server and folder
> location and then try it from there?


The user PHP runs as needs to be able to access it, not you. I'm
guessing you're on Windows...

If you're using IIS then it's the IUSR_machine user which doesn't have
access to the network by default. You can enable it but I can't recall
how off the top of my head and you may want to reconsider because it
leaves the server a lot more open should IIS/PHP/else be compromised.

If you're using Apache on Windows then you'll need to check the
service configuration to see what user it's running as.

-Stut

--
http://stut.net/
Reply With Quote
  #9 (permalink)  
Old 08-14-2008
Boyd, Todd M.
 
Posts: n/a
Default RE: [PHP] Displaying files

> -----Original Message-----
> From: Stut [mailto:stuttle@gmail.com]
> Sent: Thursday, August 14, 2008 4:21 PM
> To: Dan Shirah
> Cc: PHP-General List
> Subject: Re: [php] Displaying files
>
> On 14 Aug 2008, at 21:57, Dan Shirah wrote:
> > That simply means it can't open the file. Make sure the machine this
> > is running on has everything it needs to access that UNC filename.
> >
> > -Stut
> >
> >
> > Stut,
> >
> > If I copy the link from the error message and paste it into a
> > browser running from my PHP server, the file comes up just fine.
> >
> > Should I try mkdir() or mkpath() to set the server and folder
> > location and then try it from there?

>
> The user PHP runs as needs to be able to access it, not you. I'm
> guessing you're on Windows...
>
> If you're using IIS then it's the IUSR_machine user which doesn't have
> access to the network by default. You can enable it but I can't recall
> how off the top of my head and you may want to reconsider because it
> leaves the server a lot more open should IIS/PHP/else be compromised.
>
> If you're using Apache on Windows then you'll need to check the
> service configuration to see what user it's running as.


It can be done somewhat securely by mapping a network drive and then
granting permissions to it specifically, rather than the network itself.
(I believe...)


Todd Boyd
Web Programmer



Reply With Quote
  #10 (permalink)  
Old 08-14-2008
Stut
 
Posts: n/a
Default Re: [PHP] Displaying files

On 14 Aug 2008, at 22:24, Boyd, Todd M. wrote:

>> -----Original Message-----
>> From: Stut [mailto:stuttle@gmail.com]
>> Sent: Thursday, August 14, 2008 4:21 PM
>> To: Dan Shirah
>> Cc: PHP-General List
>> Subject: Re: [php] Displaying files
>>
>> On 14 Aug 2008, at 21:57, Dan Shirah wrote:
>>> That simply means it can't open the file. Make sure the machine this
>>> is running on has everything it needs to access that UNC filename.
>>>
>>> -Stut
>>>
>>>
>>> Stut,
>>>
>>> If I copy the link from the error message and paste it into a
>>> browser running from my PHP server, the file comes up just fine.
>>>
>>> Should I try mkdir() or mkpath() to set the server and folder
>>> location and then try it from there?

>>
>> The user PHP runs as needs to be able to access it, not you. I'm
>> guessing you're on Windows...
>>
>> If you're using IIS then it's the IUSR_machine user which doesn't
>> have
>> access to the network by default. You can enable it but I can't
>> recall
>> how off the top of my head and you may want to reconsider because it
>> leaves the server a lot more open should IIS/PHP/else be compromised.
>>
>> If you're using Apache on Windows then you'll need to check the
>> service configuration to see what user it's running as.

>
> It can be done somewhat securely by mapping a network drive and then
> granting permissions to it specifically, rather than the network
> itself.
> (I believe...)


It's been a while since I've used Windows but IIRC you need to enable
network access for that user at the lowest layer (i.e. system policy)
and then normal access rules apply, but I could be wrong. Either way
I'd avoid doing it if at all possible.

-Stut

--
http://stut.net/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 02:37 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0