Forum Moderators: coopster

Message Too Old, No Replies

"Force Download" function

         

neophyte

9:15 am on May 15, 2005 (gmt 0)

10+ Year Member



I've built a website that uses a force-download php script that's triggered by an on-click javascript event.

The script works fine when fired via javascript, but I'm trying to tweak it into an includable function that's generically portable and is triggered by an input button within an "empty" form - nothing's between the form tags but the input button and two hidden fields which feed the file name and directory path of the file to the download function.

The input trigger works, and the file name and path are passed succesfully, but I get a bunch of "can't modify header information" warnings.

The force-download script does have a bunch of header information in it.

Anyway, is there a way to make this work without using javascript?

Happy to post the code if there's any interest in seeing it.

Neophyte

webkami

4:34 pm on May 18, 2005 (gmt 0)

10+ Year Member



force download script should always be the first one to spit anything to the browser on a page.

other wise it would not work.

I am using a download.php for this purpose, I just pass the url and its starts downloading.

--------------------------------------------
<?php
$filename = // your parameter

$filename = realpath($filename); //server specific

$file_extension = strtolower(substr(strrchr($filename,"."),1));

if (! file_exists( $filename ) )
{
die("NO FILE HERE";
};
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 "jpe": 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");
header("Content-Disposition: attachment; filename=".basename($filename).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
@readfile("$filename") or die("File not found.");
exit();

?>

webkami

4:35 pm on May 18, 2005 (gmt 0)

10+ Year Member



Just put that file as download.php and make your redirect/links/form submissions pointing to it

jatar_k

7:07 pm on May 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld webkami

neophyte

9:47 am on May 19, 2005 (gmt 0)

10+ Year Member



webkami -

Thanks for your input and your script - its nearly the same code as the one I was using before but more comprehensive.

I think I'm pushing it here and maybe I'm trying to do something that just can't be done. If that's the case, I'll live with it and more than likely go back to the javascript solution, but here's what I'm trying to accomplish:

At the top of a "confirmation" file - where a user will be taken after they have registered certain personal details - are the following lines:

<?php
include ('../_include/download2.inc.php'); // this is your script which I've turned into a function.
?>

<?php
if (isset($_POST['download'])) {
$filename = $_POST['file'];

download($filename);
}
?>

Note that I've put your code in a file named download2.inc.php. I've also made your code a function: download(filename).

So, the function is the first thing on the page, followed by the conditional statement.

Then there's a form on the page with a single input button which says: "click to download the .pdf".

The form code is as follows:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="download" value="Click to Download the PDF"/>
<input type="hidden" name="file" value="d/FirstMeeting.pdf" />
</form>

So, when someone clicks the input button, the conditional statement is true and the local path and file name (d/FirstMeeting.pdf) is put into a variable ($filename) which is then passed to your script.

Then - atleast on my local machine - I get a ton of header warnings.

I appreciate your input and assistance, but am wondering if what I want to do is even possible in the way in which I want to do it.

Neophyte

neophyte

9:56 am on May 19, 2005 (gmt 0)

10+ Year Member



webkami -

Oh my God. I'm an idiot! I just figured it out. I just need to point the form action to the download file, passing the path name in a post variable.

Dohhhh!

Sorry for the previous, long-winded "oh my god I can't get this to work" post.

Thanks again for your help - next time I'll be reading instructions more closely.

Neophyte

webkami

10:25 pm on May 19, 2005 (gmt 0)

10+ Year Member



no problem,

I was reading your post and then was thinking to write the steps again, good that you got it yourself.

in your last post's way only problem was the form being on same page.

or maybe you can try it with

die(); just after the download function call coz actually when you were downloading, the code was returning to spit out the form below and bingo---- HEADER errors.

so die could have saved you there.

Anyway if its working its good :)