Auto Download issues....

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
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Auto Download issues....

Post by MiniMonty »

Hi all,

I'm trying to trigger an auto download of a .zip file which I can do with this code

Code: Select all

 
    $filename = "blahblah/bleetbleet.zip";
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: application/x-zip");
    // change, added quotes to allow spaces in filenames, by Rajkumar Singh
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");  
    exit();
 

Trouble is wherever I place it I get errors about cannot modify headers etc.,

How can I display a page with text and graphics which also triggers an auto download
of the zip file ?

Best wishes
Monty
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: Auto Download issues....

Post by davex »

Hi,

That error usually indicates that something has been output first. You say wherever you put it but I'm guessing that there is an include() or something else above?

If your entire page just has the php tag and then that code all should be well.

If you are including any files then it will be whitespace in them - I suggest you have a look at output buffering using ob_start() as the very first line in the file before any includes and then ob_end_flush just before your header output.

Cheers,

Dave.
Post Reply