0

I'm having issues force downloading a zip file which is stored on the server. I have the full script below, this script contains functions only, the function is called from a separate file where header information is sent .

function goZipper($files, $destination = '', $originPath = '', $overwrite = true) {
    if(file_exists($destination) && !$overwrite) {
        echo"File exists";
        return false; 
        }
    if(count($files)){
        //create a zip archive
        $zip = new ZipArchive();


$destination = $destination .'/'.'archive - '.date('m-d-Y_h:i-a').'.zip';
            if($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true){
                echo"Error opening destination";
                return false;
            }

            foreach($files as $file){
                $base = $originPath . $file;
                $zip->addFile($base, $file);

            }
            $zip->close();
            return($destination);
            }
            else{
                return false;
            }
    }

 function downloadZipper($fullFilePath){
 $fileName = basename($fullFilePath);

header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Length: " . filesize($fullFilePath));

readfile($fileName);
 exit;
 }

A full server path (including the file) is passed in to the function. I'm then extracting the basename for input in to the headers. I can't see any issues with this. I get the error 'Cannot modify header information - headers already sent'

Liam Fell
  • 1,308
  • 3
  • 21
  • 39
  • 2
    Cannot modify header information - headers already sent is normally seen when there is output sent to the browser, check for whitespace at the top of your files, make sure nothing has been sent to the browser before you set all your headers – Dale Nov 13 '15 at 13:44
  • Thanks, when you say whitespace at the top of files do you mean in the file-name? – Liam Fell Nov 13 '15 at 13:46
  • I mean before the opening – Dale Nov 13 '15 at 13:47
  • 1
    Can you post the whole file? – Alan Rezende Nov 13 '15 at 13:48
  • 1
    Odds are: you're calling this function in a poor location. You cannot send `header()` data *after* content has been echoed/printed. That's why you are getting this error. Take a look at the linked ticket (duplicate) for a better explanation. – Ian Nov 13 '15 at 14:21

0 Answers0