Page 1 of 1

I cannot download files other than those in plain text

Posted: Thu Jun 05, 2008 10:05 am
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.

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

Posted: Thu Jun 05, 2008 11:18 am
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');
}
 

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

Posted: Thu Jun 05, 2008 12:05 pm
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.

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

Posted: Fri Jun 06, 2008 1:19 am
by RobertGonzalez
All you need is a Content-Type header and a Content-Disposition header. What content type strings are you using?