Jump to content

Archived

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

glenelkins

download with code

Recommended Posts

How would i let a user click a link and download a file in php. This can be any file type

Share this post


Link to post
Share on other sites
Hi,

try this... just set the content dispositsion and the file name etc

[code]

<?php

  function downloadFile($thefile, $ctype, $dname) {
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-type: ".$ctype);
      header("Content-Length: ".filesize($thefile));
      header("Content-Disposition: attachment; filename=\"".$dname."\"");
      header("Content-Transfer-Encoding: binary");
      $fp = fopen($thefile);
      fpassthru($fp);
      fclose($fp);
  }

$thefile = "HSC21.pdf";
$ctype = "application/pdf";
$dname = "HSC21.pdf";

downloadFile($thefile, $ctype, $dname);
?>

[/code]

Cheers

Tim Perrett

Share this post


Link to post
Share on other sites
ok cheers. is there a function to test a files extension? as i am wanting to allow downloads of multiple file types so:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$ctype = "application/pdf";[/quote]

wot quite be good enough

Share this post


Link to post
Share on other sites
for some reason its not working. i pass the correct file name but it just seems to only want to download user.php but without the extension

Share this post


Link to post
Share on other sites
im sure you can look at that code and mash somthing togehter to work out the content disposistion using the end(explode()) methods maybe

Im not going to do it for you, thats not the name of game! lol

Tim

Share this post


Link to post
Share on other sites

×

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.