View previous topic :: View next topic |
Author |
Message |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Fri Sep 01, 2006 12:30 am Post subject: PHP Tutorial: Downloadable Scripts |
|
|
I find alot of questions regarding how to prompt a user for a PHP download. I've
created a way to easily make this happen with just two lines of code. I have not
found a better way to do this. This is too simple!
Step 01.
The first thing you need to do is add your PHP code to a simple text file, which
can be any name. I will use test.txt as an example.
Step 02.
You need to place the following code in a new PHP file, which can be called anything
you want. I will use script.php as an example.
Code: | <?php
/**
* Downloadable PHP code
* Created by: Josh (TRUSTAbyss)
*
* Provided by: trustabyss.com
*/
header("Content-Type: application/force-download");
// Add the path to your text file
@readfile("test.txt");
?>
|
Step 03:
Now all you have to do is put the PHP and text file in your htdocs. Now when
you link to the script.php file, you will get a download prompt. Hopefully this
will help someone with the distribution of their PHP scripts.
Note: This does not allow you to save processed PHP code. Only the original
code you've written locally will be saved as a download.
Last edited by TRUSTAbyss on Fri Sep 01, 2006 4:49 am; edited 15 times in total |
|
Back to top |
|
|
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
Posted: Fri Sep 01, 2006 3:31 am Post subject: |
|
|
Thanks, Josh. That's really helpful!
Here is another way to do let someone download a file. The advantage is that you can specify the default filename that someone will see when they choose to download the file.
Code: | <?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?> |
_________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
|
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Fri Sep 01, 2006 2:24 pm Post subject: |
|
|
Content-Disposition isn't supported in all IE browsers. What I did was set a new
MIME Type for the PHP file, for the browser to show as a download. |
|
Back to top |
|
|
loloyd -
Joined: 03 Mar 2006 Posts: 435 Location: Philippines
|
Posted: Mon Sep 04, 2006 10:22 am Post subject: |
|
|
Actually, "Content-Disposition" is not a valid generic HTTP/1.x header field, hence its non-wide support among HTTP/1.x-compliant browsers.
I find that declaring a "bogus" application MIME subtype like "Content-Type: application/force-download" to be elegant, convenient and simple. The application MIME type is useful enough for this purpose, to help fool clients that they have no handler for something like a force-download subtype.
Thanks for this info, TRUSTAbyss. And thanks also to pkSML for additional discussion. _________________
http://home.loloyd.com/ is online if the logo graphic at left is showing. |
|
Back to top |
|
|
|