Jump to content

PHP zip create and extract


Recommended Posts

Hello,

I am creating zip file of multiple files using PHP and downloading it.

Problem is that: zip file is getting extracted by only WinRAR, its not getti ng extracted by default windows extracter or other software.

Here is code which I have written -

            $file_folder = 'referral-resume/';
            if(extension_loaded('zip'))
            { 
                if(isset($_POST['files']) and count($_POST['files']) > 0)
                {     
                    $zip = new ZipArchive();
                    $zip_name = "referralresumes".time().".zip";
                    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
                    { 
                        $error .= "* Sorry ZIP creation failed at this time";
                    }
                    foreach($_POST['files'] as $file){ 
                        $zip->addFromString(basename($file_folder.$file),  file_get_contents($file_folder.$file));  
 
                    }
                    $zip->close();
                    if(file_exists($zip_name)){
                        header('Content-type: application/zip');
                        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                        readfile($zip_name);
                        unlink($zip_name);
                    }
 
                }
                else{
                    $error .= "* Please select file to zip ";
                }    
            }
            else{
                $error .= "* You dont have ZIP extension";
            }
 

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.