Languages :: PHP :: how to download file....(any kind) |
|||
| By: jigs77 |
Date: 14/05/2003 00:00:00 |
Points: 20 | Status: Answered Quality : Excellent |
|
hello everyone, In a download section of my site i want to give download option of file, for any type of download so kindly help me what code i used to download the various kind of file format????? |
|||
| By: Louskan | Date: 14/05/2003 21:57:00 | Type : Comment |
|
| What do you want in fact ? Caus' if it's just download, a link to the file should be enough. If you want it to be automatic, you can use an iframe This code is used by download.com website: <iframe height="0" width="0" src="<A HREF="http://www.clasys.com/iturbo54.exe">http://www.clasys.com/iturbo54.exe</A>"></iframe> |
|||
| By: carchitect | Date: 14/05/2003 22:08:00 | Type : Comment |
|
| correct |
|||
| By: VGR | Date: 14/05/2003 22:12:00 | Type : Answer |
|
| not exactly. if the type is known to the browser, clicking (not right-clicking + save as ! ;-) will not download but "execute" the document. This may be unpleasant you should force download this way : either 1) use this before sending the data : header("Content-type: application/application/save"); or 2) header("Content-disposition: attachment;;filename=$filename"); header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); $client=getenv("HTTP_USER_AGENT"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); echo $str; fclose($fp); |
|||
| By: girish_nair | Date: 14/05/2003 23:09:00 | Type : Comment |
|
| Hi, VGR is correct! Also you can save some lines by using readfile("$filename"); instead of $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); echo $str; fclose($fp); :) |
|||
| By: VGR | Date: 14/05/2003 23:22:00 | Type : Comment |
|
| the only difference is that the "r" should be in fact "rb", something readfile() can not do. If the data is binary-unsafe... moreover, I made a typo above in here : header("Content-type: application/application/save"); I let you find it :D |
|||
| By: OwenMania | Date: 19/05/2003 13:41:00 | Type : Comment |
|
| if i wish to the user to download from an FTP server which command should i pick? the url for a file is like this: <A HREF="ftp://name:password@202.111.111.22:21/filename.txt">ftp://name:password@202.111.111.22:21/filename.txt</A> how to prompt user for download path, instead of letting the browser open it? thank u very much |
|||
| By: VGR | Date: 19/05/2003 14:51:00 | Type : Comment |
|
| that's the purpose of the application/save MIME-Type that's also the purpose of the application/octetstream wadduyamean ? |
|||
| By: OwenMania | Date: 20/05/2003 00:00:00 | Type : Comment |
|
| oh i will try tat out later i got a Q if the download process is accidentally terminated how to i use code to make it resume? |
|||
| By: OwenMania | Date: 20/05/2003 00:02:00 | Type : Comment |
|
| to VGR what i meant was does your code apply to FTP file transfer as well like this path... <A HREF="ftp://name:password@202.111.111.22:21/filename.txt">ftp://name:password@202.111.111.22:21/filename.txt</A> |
|||
| By: VGR | Date: 20/05/2003 00:11:00 | Type : Comment |
|
| AFAIK, yes (fopen wrappers) it applies to this kind of URI because you in fact use HTTP not FTP by doing this. For the "resume", I'm afraid I don't have a solution. |
|||
| By: Louskan | Date: 20/05/2003 00:24:00 | Type : Comment |
|
| OwenMania, it's not even your own question. I think you should open a new thread for that. |
|||
| By: OwenMania | Date: 20/05/2003 16:04:00 | Type : Comment |
|
| pardon me because i am newbie how to implement this thing in an ASp page? header("Content-disposition: attachment;;filename=$filename"); header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); do i type it in meta tag? |
|||
| By: VGR | Date: 20/05/2003 16:32:00 | Type : Comment |
|
| yes, that's a solution. Those are HTTP header fields. I suppose they can be coded as HTTP-META-TAGS, especially the Pragma: no-cache But this is the PHP area, you know ? :D That's why you got PHP code, not ASP code :D perhaps you would receive more help from the ASP area guys ;-) |
|||
| By: OwenMania | Date: 20/05/2003 16:40:00 | Type : Comment |
|
| oh no wonder it cannot work thank u very very much for your comments |
|||
| By: Louskan | Date: 20/05/2003 16:45:00 | Type : Comment |
|
| Won't <META Http-Equiv="Content-disposition" Content="attachment;filename=file.ext"> <META Http-Equiv="Content-type" Content="application/octetstream"> <META Http-Equiv="Pragma" Content="no-cache"> <META Http-Equiv="Expires" Content="0"> written between <head></head> do the trick? Well I'm not sure, if someone could test! No time for that today :p See ya. |
|||
| By: VGR | Date: 20/05/2003 16:50:00 | Type : Comment |
|
| absolutely :D it should work |
|||
| By: adnan1983 | Date: 22/05/2003 06:33:00 | Type : Comment |
|
| Hi, I have been trying to download a file using this code. What this is code does is takes the file path from the database, and then generates a link out of it. But it doesn't download anything. I tried to use header("Content-type: application/application/save"); , but kept saying cannot add header information, is it because of all the 'echo' statements. Can someone please tell me where exactly I have to add the header? $retrieve = "SELECT * FROM files"; $run = mysql_query ($retrieve); $result = mysql_fetch_array ($run); echo " <b>Filename from database, download file at link: </b> "; $realpath = realpath ($uploadPath); echo "<a href=\".$realpath.\">click</a>"; |
|||
| By: VGR | Date: 22/05/2003 07:23:00 | Type : Comment |
|
| application/application/save is a typo should read application/save |
|||
| By: adnan1983 | Date: 22/05/2003 07:29:00 | Type : Comment |
|
| Where exactly should I place header("Content-type: application/save"); in this code, cause I keep getting "warning: cannot add header information." ? Thanks. if($file_size == 0) { echo "<font color=#FF0000>Error: No file Uploaded.</font>"; exit; } else { $uploadPath = "/home/virtual/site70/fst/var/www/html/foo/uploads/".$file_name; copy ($file, $uploadPath); $db = mysql_connect ("localhost", "foo", "goo") ; if (!$db) { echo "<font color=#FF0000>Cannot connect to the database at this moment, please try later again.</font>"; } else { mysql_select_db ("somedatabase"); $file_info = "INSERT INTO files VALUES ('".$id."', '".$file_name."')"; $data_entered = mysql_query ($file_info); $retrieve = "SELECT * FROM files"; $run = mysql_query ($retrieve); $result = mysql_fetch_array ($run); echo " <b>Filename from database, download file at link: </b> "; header("Content-type: application/save"); <----I have placed it her right before downloading. $realpath = "uploads/".$file_name; echo "<a href=".$realpath.">click</a>"; } } |
|||
| By: VGR | Date: 22/05/2003 07:46:00 | Type : Comment |
|
| you can't add header() if any output has begin. In your case, it's this posing the problem : echo " <b>Filename from database, download file at link: </b> "; no, you don't need the header(). You've to choose either to display a <a href= link OR to send directly the data (in which case a header() for the type is welcome :D ) it's your choice. I recommend to download directly, this way : if($file_size == 0) { echo "<font color=#FF0000>Error: No file Uploaded.</font>"; exit; } else { $uploadPath = "/home/virtual/site70/fst/var/www/html/foo/uploads/".$file_name; copy ($file, $uploadPath); $db = mysql_connect ("localhost", "foo", "goo") ; if (!$db) { // useless echo "<font color=#FF0000>Cannot connect to the database at this moment, please try later again.</font>"; } else { mysql_select_db ("somedatabase"); $file_info = "INSERT INTO files VALUES ($id,'$file_name');"; // double quotes have this purpose :D else use single ones (faster) I suppose $id is a numeric value :D $data_entered = mysql_query ($file_info); /* retrieval is useless, you've the data in memory ! $retrieve = 'SELECT * FROM files;'; $run = mysql_query ($retrieve); $result = mysql_fetch_array ($run); */ header('Content-type: application/save'); readfile($uploadPath); // or $file exit; // you've to make sure output is flushed and the script ends } // if connection ok } // if filesize=0 |
|||
| By: VGR | Date: 22/05/2003 07:48:00 | Type : Comment |
|
| if you choose the wa href= solution, the user won't be prompted for "save as..." except if he does right-click/save as... manually |
|||
| By: adnan1983 | Date: 22/05/2003 07:51:00 | Type : Comment |
|
| coool, thanks for the help. Let me try this, if not I will be back. |
|||
| By: adnan1983 | Date: 22/05/2003 08:05:00 | Type : Comment |
|
| thanks it works now. |
|||
| By: adnan1983 | Date: 22/05/2003 08:05:00 | Type : Comment |
|
| but is there a possible to download it, when a link is clicked. Using the same method u posted. |
|||
| By: VGR | Date: 22/05/2003 08:20:00 | Type : Comment |
|
| I told you already. Unless you find a way to "force a right-click" when cliking a link, you can't with the link you tried first. BUT you can, in fact, with this technique Home All Topics Web Development Web Languages PHP Viewing a Question Edit Quick Links • Linux • MS-DOS • Windows 2000 • Windows NT • Mysql • Programming • .NET • C# • Delphi Programming • JavaScript • Pascal • PHP • Math & Science Title: how to download file....(any kind) From: jigs77 Points: 20 Date: 05/14/2003 04:09AM PDT Type: Open hello everyone, In a download section of my site i want to give download option of file, for any type of download so kindly help me what code i used to download the various kind of file format????? Question History Comment from Louskan Date: 05/14/2003 04:57AM PDT What do you want in fact ? Caus' if it's just download, a link to the file should be enough. If you want it to be automatic, you can use an iframe This code is used by download.com website: <iframe height="0" width="0" src="<A HREF="http://www.clasys.com/iturbo54.exe">http://www.clasys.com/iturbo54.exe</A>"></iframe> Comment from carchitect Date: 05/14/2003 09:08AM PDT correct Comment from VGR Date: 05/14/2003 09:12AM PDT not exactly. if the type is known to the browser, clicking (not right-clicking + save as ! ;-) will not download but "execute" the document. This may be unpleasant you should force download this way : either 1) use this before sending the data : header("Content-type: application/application/save"); or 2) header("Content-disposition: attachment;;filename=$filename"); header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); $client=getenv("HTTP_USER_AGENT"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); echo $str; fclose($fp); Comment from girish_nair Date: 05/14/2003 11:09AM PDT Hi, VGR is correct! Also you can save some lines by using readfile("$filename"); instead of $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); echo $str; fclose($fp); :) Comment from VGR Date: 05/14/2003 11:22AM PDT the only difference is that the "r" should be in fact "rb", something readfile() can not do. If the data is binary-unsafe... moreover, I made a typo above in here : header("Content-type: application/application/save"); I let you find it :D Comment from OwenMania Date: 05/19/2003 08:41PM PDT if i wish to the user to download from an FTP server which command should i pick? the url for a file is like this: <A HREF="ftp://name:password@202.111.111.22:21/filename.txt">ftp://name:password@202.111.111.22:21/filename.txt</A> how to prompt user for download path, instead of letting the browser open it? thank u very much Comment from VGR Date: 05/19/2003 09:51PM PDT that's the purpose of the application/save MIME-Type that's also the purpose of the application/octetstream wadduyamean ? Comment from OwenMania Date: 05/20/2003 07:00AM PDT oh i will try tat out later i got a Q if the download process is accidentally terminated how to i use code to make it resume? Comment from OwenMania Date: 05/20/2003 07:02AM PDT to VGR what i meant was does your code apply to FTP file transfer as well like this path... <A HREF="ftp://name:password@202.111.111.22:21/filename.txt">ftp://name:password@202.111.111.22:21/filename.txt</A> Comment from VGR Date: 05/20/2003 07:11AM PDT AFAIK, yes (fopen wrappers) it applies to this kind of URI because you in fact use HTTP not FTP by doing this. For the "resume", I'm afraid I don't have a solution. Comment from Louskan Date: 05/20/2003 07:24AM PDT OwenMania, it's not even your own question. I think you should open a new thread for that. Comment from OwenMania Date: 05/20/2003 11:04PM PDT pardon me because i am newbie how to implement this thing in an ASp page? header("Content-disposition: attachment;;filename=$filename"); header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); do i type it in meta tag? Comment from VGR Date: 05/20/2003 11:32PM PDT yes, that's a solution. Those are HTTP header fields. I suppose they can be coded as HTTP-META-TAGS, especially the Pragma: no-cache But this is the PHP area, you know ? :D That's why you got PHP code, not ASP code :D perhaps you would receive more help from the ASP area guys ;-) Comment from OwenMania Date: 05/20/2003 11:40PM PDT oh no wonder it cannot work thank u very very much for your comments Comment from Louskan Date: 05/20/2003 11:45PM PDT Won't <META Http-Equiv="Content-disposition" Content="attachment;filename=file.ext"> <META Http-Equiv="Content-type" Content="application/octetstream"> <META Http-Equiv="Pragma" Content="no-cache"> <META Http-Equiv="Expires" Content="0"> written between <head></head> do the trick? Well I'm not sure, if someone could test! No time for that today :p See ya. Comment from VGR Date: 05/20/2003 11:50PM PDT absolutely :D it should work Comment from adnan1983 Date: 05/22/2003 01:33PM PDT Hi, I have been trying to download a file using this code. What this is code does is takes the file path from the database, and then generates a link out of it. But it doesn't download anything. I tried to use header("Content-type: application/application/save"); , but kept saying cannot add header information, is it because of all the 'echo' statements. Can someone please tell me where exactly I have to add the header? $retrieve = "SELECT * FROM files"; $run = mysql_query ($retrieve); $result = mysql_fetch_array ($run); echo " <b>Filename from database, download file at link: </b> "; $realpath = realpath ($uploadPath); echo "<a href=\".$realpath.\">click</a>"; Comment from VGR Date: 05/22/2003 02:23PM PDT application/application/save is a typo should read application/save Comment from adnan1983 Date: 05/22/2003 02:29PM PDT Where exactly should I place header("Content-type: application/save"); in this code, cause I keep getting "warning: cannot add header information." ? Thanks. if($file_size == 0) { echo "<font color=#FF0000>Error: No file Uploaded.</font>"; exit; } else { $uploadPath = "/home/virtual/site70/fst/var/www/html/foo/uploads/".$file_name; copy ($file, $uploadPath); $db = mysql_connect ("localhost", "foo", "goo") ; if (!$db) { echo "<font color=#FF0000>Cannot connect to the database at this moment, please try later again.</font>"; } else { mysql_select_db ("somedatabase"); $file_info = "INSERT INTO files VALUES ('".$id."', '".$file_name."')"; $data_entered = mysql_query ($file_info); $retrieve = "SELECT * FROM files"; $run = mysql_query ($retrieve); $result = mysql_fetch_array ($run); echo " <b>Filename from database, download file at link: </b> "; header("Content-type: application/save"); <----I have placed it her right before downloading. $realpath = "uploads/".$file_name; echo "<a href=".$realpath.">click</a>"; } } Comment from VGR Date: 05/22/2003 02:46PM PDT you can't add header() if any output has begin. In your case, it's this posing the problem : echo " <b>Filename from database, download file at link: </b> "; no, you don't need the header(). You've to choose either to display a <a href= link OR to send directly the data (in which case a header() for the type is welcome :D ) it's your choice. I recommend to download directly, this way : if($file_size == 0) { echo "<font color=#FF0000>Error: No file Uploaded.</font>"; exit; } else { $uploadPath = "/home/virtual/site70/fst/var/www/html/foo/uploads/".$file_name; copy ($file, $uploadPath); $db = mysql_connect ("localhost", "foo", "goo") ; if (!$db) { // useless echo "<font color=#FF0000>Cannot connect to the database at this moment, please try later again.</font>"; } else { mysql_select_db ("somedatabase"); $file_info = "INSERT INTO files VALUES ($id,'$file_name');"; // double quotes have this purpose :D else use single ones (faster) I suppose $id is a numeric value :D $data_entered = mysql_query ($file_info); /* retrieval is useless, you've the data in memory ! $retrieve = 'SELECT * FROM files;'; $run = mysql_query ($retrieve); $result = mysql_fetch_array ($run); */ echo " <b>Filename from database, download file at link: </b> "; $realpath = "uploads/".$file_name; echo "<a href=download.php?filepath=\"$realpath\">click</a>"; // you can add a target="_new" (see option (3) below in download.php ) } // if connection ok } // if filesize=0 ---------- download.php : <?php $filepath=$_GET['filepath']; header('Content-type: application/save'); readfile($filepath); // or $file // 1) exit; // you've to make sure output is flushed and the script ends // or 2) I suggest to redirect // or 3) if you opened this with a target= in the calling script, do : // echo '<script>self.close;</script>'; ?> |
|||
| By: lumpofmilk | Date: 04/06/2003 21:00:00 | Type : Comment |
|
| I am using this code <?php $filename = $_GET['filename']; header("Content-disposition: attachment;;filename=$filename"); header("Content-type: x-pn-multirate-realvideo"); header("Pragma: no-cache"); header("Expires: 0"); $client=getenv("HTTP_USER_AGENT"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); echo $str; fclose($fp); ?> But when I try to download a file, the download stops at about 400-500kb, when the actual file size is 4mb... I was downloading at 30kb/sec what could be wrong? |
|||
| By: VGR | Date: 04/06/2003 21:13:00 | Type : Comment |
|
| the default maximum execution time of 30 seconds ? use "set_time_limit(0);" before downloading (better code will afterwards reset it to the gp* previous value, that you can get with ini_get() or something like that) |
|||
| By: lumpofmilk | Date: 04/06/2003 22:54:00 | Type : Comment |
|
| I am new to PHP (I code in ASP) so can you please tell me where to put this line in the code? I did this: <?php $filename = $_GET['filename']; header("Content-disposition: attachment;;filename=$filename"); header("Content-type: x-pn-multirate-realvideo"); header("Pragma: no-cache"); header("Expires: 0"); set_time_limit(0); $client=getenv("HTTP_USER_AGENT"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); echo $str; fclose($fp); ?> but it still didn't help and my download stoppped after 15 seconds. |
|||
| By: VGR | Date: 04/06/2003 23:02:00 | Type : Comment |
|
| sorry for three places above, it looks like I missed a copy-paste :D for the place where to put the set_time_limit : anywhere before the echo $str ;-) why not where it is now. More "logical" would be on the very first line of the script do as you wish. |
|||
| By: VGR | Date: 04/06/2003 23:04:00 | Type : Comment |
|
| please note $client= line is useless "stopped after 15 seconds" ? hummm how weird do you use IE6 ? :D Patched with SP1 ? :D |
|||
| By: lumpofmilk | Date: 04/06/2003 23:34:00 | Type : Comment |
|
| I'm using IE 5.5 |
|||
| By: VGR | Date: 04/06/2003 23:42:00 | Type : Comment |
|
| ok, so it's not one of the infamous IE6 problems. could you try this ? <?php set_time_limit(0); // note that this may fail, and will if you're on "safe mode" !! $filename = $_GET['filename']; header("Content-disposition: attachment;;filename=$filename"); header("Content-type: application/save"); header("Pragma: no-cache"); header("Expires: 0"); $client=getenv("HTTP_USER_AGENT"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); fclose($fp); echo $str; ?> |
|||
| By: lumpofmilk | Date: 05/06/2003 00:13:00 | Type : Comment |
|
| No, still no change.. when I test this on netscape it downloads it in less than a second as a .php file and on IE 5.5 it downloads about 400-500kb as .rm file |
|||
| By: VGR | Date: 05/06/2003 00:31:00 | Type : Comment |
|
| ok, why not this then ? <?php set_time_limit(0); // note that this may fail, and will if you're on "safe mode" !! $filename = $_GET['filename']; header("Content-disposition: attachment; filename=$filename"); header("Content-type: application/save"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); fclose($fp); echo $str; ?> |
|||
| By: lumpofmilk | Date: 06/06/2003 00:11:00 | Type : Comment |
|
| still the same..... maybe its a server problem? |
|||
| By: VGR | Date: 06/06/2003 00:49:00 | Type : Comment |
|
| ok, now this : <?php set_time_limit(0); // note that this may fail, and will if you're on "safe mode" !! $filename = $_GET['filename']; header("Content-disposition: attachment;;filename=$filename"); header("Content-type: application/octetstream"); $fp=fopen("$filename","r"); $str=fread($fp,filesize($filename)); fclose($fp); echo $str; ?> |
|||
| By: lumpofmilk | Date: 07/06/2003 00:00:00 | Type : Comment |
|
| nope.. |
|||
| By: VGR | Date: 07/06/2003 00:09:00 | Type : Comment |
|
| then may-be a server config problem... Or you are in safe_mode |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








