Jump to content

Automatically download a file?


pinacoladaxb

Recommended Posts

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

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.

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

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.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.