Jump to content


Photo

download with code


  • Please log in to reply
4 replies to this topic

#1 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 27 February 2006 - 09:11 AM

How would i let a user click a link and download a file in php. This can be any file type
http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 tim_perrett

tim_perrett
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationBath, UK

Posted 27 February 2006 - 09:52 AM

Hi,

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


<?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);
?>


Cheers

Tim Perrett
[!--coloro:gray--][span style="color:gray"][!--/coloro--][!--fonto:Arial--][span style="font-family:Arial"][!--/fonto--][!--sizeo:1--][span style="font-size:8pt;line-height:100%"][!--/sizeo--]Tim Perrett[!--sizec--][/span][!--/sizec--]

-- PHP 5.1 --
-- Ruby 1.8.4 / Rails 1.1 --
-- LigHTTPD 1.4.1 --
-- FastCGI -- -- Apache 2.0.2 -- -- Apple OSX -- -- Linux: FC5 / SUSE -- -- JavaScript -- -- Objective C -- -- CGI / Perl -- -- MySQL 5 -- -- PostGre SQL -- -- Java (Spring) -- -- Unix -- -- Win32 -- "90% of being smart is knowing what you're dumb at." [!--colorc--][/span][!--/colorc--][!--fontc--][/span][!--/fontc--]

#3 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 27 February 2006 - 10:16 AM

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
http://thewebsolutionprovider.com - A Solution For Everything Web!

#4 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 27 February 2006 - 11:36 AM

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
http://thewebsolutionprovider.com - A Solution For Everything Web!

#5 tim_perrett

tim_perrett
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationBath, UK

Posted 26 June 2006 - 03:00 PM

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
[!--coloro:gray--][span style="color:gray"][!--/coloro--][!--fonto:Arial--][span style="font-family:Arial"][!--/fonto--][!--sizeo:1--][span style="font-size:8pt;line-height:100%"][!--/sizeo--]Tim Perrett[!--sizec--][/span][!--/sizec--]

-- PHP 5.1 --
-- Ruby 1.8.4 / Rails 1.1 --
-- LigHTTPD 1.4.1 --
-- FastCGI -- -- Apache 2.0.2 -- -- Apple OSX -- -- Linux: FC5 / SUSE -- -- JavaScript -- -- Objective C -- -- CGI / Perl -- -- MySQL 5 -- -- PostGre SQL -- -- Java (Spring) -- -- Unix -- -- Win32 -- "90% of being smart is knowing what you're dumb at." [!--colorc--][/span][!--/colorc--][!--fontc--][/span][!--/fontc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users