Jump to content

Download file - major issue


glenelkins

Recommended Posts

[code]
function downloadFile($thefile) {
    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-Length: ".filesize($thefile));
    header ("Content-Disposition: attachment; filename=".$thefile);
    header ("Content-Transfer-Encoding: binary");
    
    $fopen = fopen($thefile);
    fpassthru($fopen);
    fclose ($fopen);
}

downloadFile("filename");
[/code]

Ok when this code is run it is supposed to download the file. It does exactly that. but, this code is on a page called users.php, all it does is download the internet explorer source for this pages code. Why is it doing this
Link to comment
https://forums.phpfreaks.com/topic/3837-download-file-major-issue/
Share on other sites

Well its a file manager. I want the user to click the download button next to any file and download the file. I tested this with a zip file also, it downloaded the file but would not open it. I then tried a php file and it downloaded it but not the actual code file but the source from IE. Dont know why its doing this. any file i download it wont open it or it opens as a load of crap
Well, I'm not an fopen expert, but I believe you have 2 problems here. First, .zip is a binary format and you're streaming ascii data to the file. You need to use fopen($filename, 'rb') to open a binary file.

For the .php file problem, when it "downloads", I'm not sure.. I tried it here and I get the raw code, as expected..

Also, as a side note, I get an error if I call fopen with only one argument. The string mode argument is required.

Archived

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

×
×
  • Create New...

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.