PHP Tutorial: Downloadable Scripts

 
Post new topic   Reply to topic    Aprelium Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Fri Sep 01, 2006 12:30 am    Post subject: PHP Tutorial: Downloadable Scripts Reply with quote

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 View user's profile Send private message Visit poster's website
pkSML
-


Joined: 29 May 2006
Posts: 952
Location: Michigan, USA

PostPosted: Fri Sep 01, 2006 3:31 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Fri Sep 01, 2006 2:24 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
loloyd
-


Joined: 03 Mar 2006
Posts: 435
Location: Philippines

PostPosted: Mon Sep 04, 2006 10:22 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group