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
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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.