I cannot download files other than those in plain text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wjmimi
Forum Newbie
Posts: 2
Joined: Thu Jun 05, 2008 9:51 am

I cannot download files other than those in plain text

Post by wjmimi »

Everah | Please use bbCode tags when posting code in the forums.

Hi,

I combined the info in several online tutorials and finally managed to make my upload/download feature work, but when I try to download files, I can only download or open files in plain text correctly. All the files in any other format are broken. The size looks fine, but the format is messed up. I try to type in specific file types instead of using $type variable, but it does not change anything. Below are the code I am using. They may appear silly :|, but please give some suggestions. Thank you very much.
---------------------------------------------------------------

Code: Select all

//connect to db and run an query to get the file info above this point.
 
list($filename, $type, $size, $filePath) = $result->fetchRow(); 
        
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $size");
header("Content-Type: $type");
header("Accept-Ranges: bytes");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-transfer-encoding: binary");
    
readfile($filePath);
exit;
---------------------------------------------------------

Thank you again.


Everah | Please use bbCode tags when posting code in the forums.
Last edited by RobertGonzalez on Fri Jun 06, 2008 1:16 am, edited 1 time in total.
Reason: Tags note
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: I cannot download files other than those in plain text

Post by Jaxolotl »

I use this code to do so (adapted to your code):

Code: Select all

 
 list($filename, $type, $size, $filePath) = $result->fetchRow(); 
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=".str_replace(" ","_",$filename)."");
        header("Content-Length: ".$size);
        header("Accept-Ranges: bytes");
        header("Pragma: no-cache");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-transfer-encoding: binary");
 
        if(@readfile($filePath.$filename)){
        exit();
        }
else{
header('Location: write here the page where you want to go if error happens');
}
 
wjmimi
Forum Newbie
Posts: 2
Joined: Thu Jun 05, 2008 9:51 am

Re: I cannot download files other than those in plain text

Post by wjmimi »

Hi,

I tried your code. The problem was not solved. :|
Anyway, thank you very much. :)
---------------------------------------
BTW, for everybody who might be able to help, I am sure the upload is fine. I uploaded sevaral files including images, excel files, and word files. They are all ok after being uploaded to the destination folder. The problem only happens when I try to download them.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: I cannot download files other than those in plain text

Post by RobertGonzalez »

All you need is a Content-Type header and a Content-Disposition header. What content type strings are you using?
Post Reply