Re: implementing downloads with php?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Apr 2, 2015, at 7:55 PM, Sean Greenslade wrote:

On Thu, Apr 02, 2015 at 07:01:59PM -0700, Jeffry Killen wrote:
On Apr 2, 2015, at 6:33 PM, Sean Greenslade wrote:
A quick google turned up this result:

http://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header

See if that helps.

--Sean

Thank you for the link, I am getting an idea of what is going on here, even
though I don't
know that the posters problem was solved.

So, if I understand the basics,

a bunch of headers are sent and then the file is read. But I don't see a
print or echo statement
in any of the code in the related posts.

The header set functions need to be run before the actual content of the
page is generated. This has to do with the way that the HTTP protocol
works. The header is always generated by the webserver, and PHP has the option of modifying / appending to it before it gets sent. This all must happen before the content of the page gets sent, becase the server must
finish and send the headers before it sends any content. Once there is
content, the headers are locked for that pageload.

I have read a file as a string, sent a Content-Type header and then used a
print statement to send
the file contents for an async request: in cases where css or javascript is
requested after a page
loads. The output is sent to the browser.

I'm a bit confused by this, so you'll need to clarify. Setting the
headers to inform the browser that the file is a download has nothing to
do with js, css, or html. It's a built-in feature of the browser to
interpret the headers and decide to download the file instead of display it. All you need to take advantage of this is to place a link to the PHP page that supplies the file and let the user click it. If the page sets
the headers correctly, clicking that link will cause thier browser to
start downloading the file (and, depending on browser settings, possibly
offer a save location dialog).

If you're attempting to achieve this using an ajax solution, I'd
actually recommend not trying to load the file by ajax. Just use
javascript to open a new window to the download link. Using the same
mechanism as above, the browser will handle things for you.

No I wasn't intending to use ajax per-se. I was just using that as a parallel to illustrate using a print statement to actually send the file contents.
Is it  necessary to call print or echo here?

I would print a directory listing of a directory containing downloadable files
to the browser with php, where each file listing is wrapped in an anchor
that has the href value set to a script with a query attached: something
along the lines of <a href="downloader.php?file=[file to download]">[file to download]</a>

I am thinking of any file type: .pdf, .rtf, .swf, .doc, mpg, mpeg, mp3, binary, as well as plain text, whatever


But this situation appears different. The file content is sent to the
browser but the browser writes
it directly to a separate file (?)

JK

Note that there is no way to specify where the file gets downloaded.
That is up to the browser, and based on its settings. Some browsers
default to asking you where to save the file, and others toss all files
into a Downloads directory.

--Sean


Yes, the destination is generally set as a browser preference by the user. I often get a dialog ( on Mac OSX, Firefox and others) asking me if I want to open the file, or save it IF it is a pdf file. Otherwise it just appears in an
"httpDownloads" folder my desktop according to my preference setting.
JK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux