Topic: How do you make a link to cause a download |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: The Soft Cell |
posted 06-20-2006 21:01
I have a few things on my personal web space that I want other people to be able to download, eg a pdf or mp3 file. |
Paranoid (IV) Inmate From: Norway |
posted 06-20-2006 21:20 |
Lunatic (VI) Inmate From: under the bed |
posted 06-20-2006 21:49
Of course, just for the record, the user *always* has the option of whether to open or save a link (via the right-click menu...) |
Paranoid (IV) Inmate From: The Soft Cell |
posted 06-20-2006 21:49
hi poi, thanks for the reply |
Paranoid (IV) Inmate From: The Soft Cell |
posted 06-20-2006 22:58
if I purchase an ebook for example from ebay, the seller sends me an email with a link. I click the link and the download window opens. That is what I'm trying to do |
Maniac (V) Mad Librarian From: Seoul, Korea |
posted 06-21-2006 09:04
To clarify what poi said (maybe), if you are using PHP you can take a look at the manual page on header for more information. If you are not using PHP or another server-side scripting language, it is not possible to tell the server to return files with a certain header type. Instead, you could just go with the old "right-click to save" text (as DL mentioned). Honestly, I think the latter is your best option here. Give the user the choice. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 06-21-2006 10:05
Actually, Suho, if you were using Apache, you probably could tell which header to send via .htaccess. |
Paranoid (IV) Inmate From: The Soft Cell |
posted 06-21-2006 11:19
this is more complex than I expected. I am aware that if I zip the file before uploading then it will download as I want but I was hoping to avoid zipping |
Paranoid (IV) Inmate From: Norway |
posted 06-21-2006 12:41 |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 06-21-2006 13:10
Back to my .htaccess variant, code: AddType application/octet-stream pdf
code: <Files *.pdf> ForceType application/octet-stream Header set Content-Disposition attachment </Files>
|
Maniac (V) Mad Librarian From: Seoul, Korea |
posted 06-21-2006 13:52
TP: good point, .htaccess would work on apache, but unless he wants all of a certain type of file to prompt a download, it might get sticky with a lot of different rules. |
Bipolar (III) Inmate From: Australia |
posted 06-21-2006 17:53
While the right click to save thing is fine, some times you want certain functionality - and alot of users dont actually use the right click... There is nothing wrong with a download box since it still gives you the option to open or save. code: $fileName = 'myfile'; $fileExtension = 'pdf'; $sourceFile = $fileName . ' . ' . $fileExtension; $outputFile = $sourceFile; // the name they save as can be different to the existing file // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); switch( $fileExtension) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } session_cache_limiter(""); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); header("Content-Disposition: attachment; filename=".basename($outputFile).";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($sourceFile)); @readfile("$sourceFile") ; // This is the bit that prompts for the download, supress errors for niceness exit();
|
Maniac (V) Mad Librarian From: Seoul, Korea |
posted 06-22-2006 03:54
Great example code, but I'm just getting the feeling that Morph might not be using PHP, and (even if he is) that he was looking for something simple (like something you can just add to a link). In terms of simplicity, reminding users that they can use the context menu to download is probably the way to go. |
Lunatic (VI) Inmate From: under the bed |
posted 06-22-2006 05:23
^ I heartily agree, Suho. |
Bipolar (III) Inmate From: Australia |
posted 06-22-2006 06:21
Yeh well it entirely depends on what he wants to do, if he is using php could just throw that code in a download.php file then have some (securely checked) get/post variable to determine the file to download. |
Bipolar (III) Inmate From: The Leather Wheeliechair |
posted 02-13-2007 08:45
well he may not be using it, but I'll snatch that sample code if you don't mind and put it to some use. |