Jump to content

readfile in php isn't downloading file


treeleaf20

Recommended Posts

All,

I've got the following code to force a file download for a zipfile that I just created:

    $zip_name = 'download.zip';
    $result = create_zip($other_files_to_zip,$zip_name,true);
    if($result){
    echo "The filesize is: ".filesize($zip_name);
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename=filename.zip');
    header('Content-Length: ' . filesize($zip_name));
    readfile($zip_name);
    }

When I echo out the filesize it shows a correct size and it prompts me to download the filename.zip which is ok since it's just a filename but then when I save the document and try and open Windows says that Windows cannot open the folder because the folder is invalid.

 

Any ideas on how to resolve an issue like this?

 

Thanks in advance!

Link to comment
Share on other sites

You can't output a string if you are outputting the zip file's contents. It will get appended to the binary data of the zip file, potentially corrupting it.

 

Your only output should be the contents of the zip file.

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.