pinacoladaxb Posted June 21, 2008 Share Posted June 21, 2008 On my page, I want the user to click on a link and download a file. I also want to add 1 to a value in a MySQL table (downloads) so that I know how many times it has been downloaded. What I did is I made this html form: <form action="index.php" method="post" style="padding:0; margin:0"> <INPUT TYPE="hidden" NAME="do" VALUE="download"> <input type="submit" value="Download"> </form> The user clicks the download button, which refreshes the page but also adds do="download," which is a hidden field in the form. The page detects that do is equal to "download" and it adds 1 to the value in my database. My problem is that I don't want the user to know that all of this is happening. I want them to think that the button was a link to the download file. How can I make the file automatically start downloading? Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/ Share on other sites More sharing options...
Orio Posted June 21, 2008 Share Posted June 21, 2008 You script should start by checking if there should be a download. If so, update the database records and then force download the file. If a download was not requested, show the form. <?php if((isset($_POST['do']) && $_POST['do'] == "Download") { //Do the query that adds 1 to mysql //Force download: $filename = "...."; //Your filename.... // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // addition by Jorg Weske $file_extension = strtolower(substr(strrchr($filename,"."),1)); switch( $file_extension ) { 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"; } 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"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); } else { //Show the form, the file doesn't need to be downloaded } ?> Credit for the force download code piece: http://elouai.com/force-download.php Orio. Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/#findComment-570830 Share on other sites More sharing options...
pinacoladaxb Posted June 21, 2008 Author Share Posted June 21, 2008 When I tried that, I got this message when I clicked the download button: Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 348 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 349 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 350 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 351 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 352 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 354 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 355 Warning: filesize() [function.filesize]: stat failed for http://www.flufferfluff.com/fretsonfire/uploads/.zip in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 356 Warning: Cannot modify header information - headers already sent by (output started at /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php:59) in /home/pauliuko/public_html/flufferfluff/fretsonfire/index.php on line 356 Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/#findComment-570833 Share on other sites More sharing options...
Orio Posted June 21, 2008 Share Posted June 21, 2008 You didn't put that at your top of your script. You can't have output when downloading the file. Orio. Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/#findComment-570836 Share on other sites More sharing options...
pinacoladaxb Posted June 21, 2008 Author Share Posted June 21, 2008 Ok, thank you! Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/#findComment-570839 Share on other sites More sharing options...
pinacoladaxb Posted June 21, 2008 Author Share Posted June 21, 2008 NEW PROBLEM! When I click the download button, I get an error message. I checked the uploaded file. It seems fine. EDIT: Here's the exact message I get: Unable to unarchive "filename.zip" into "Desktop". (Error 1 -Operation not permitted.) Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/#findComment-570870 Share on other sites More sharing options...
pinacoladaxb Posted June 21, 2008 Author Share Posted June 21, 2008 I fixed it. It turned out that I was asking for the filename "/uploads/file.zip" rather than "uploads/file.zip." One slash makes a huge difference! Link to comment https://forums.phpfreaks.com/topic/111219-automatically-download-a-file/#findComment-570879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.