0

I am using this for reference:

How to [recursively] Zip a directory in PHP?

I run it on my website. its not works as expected. it doesn't make different folder as defined in souce(path).

I want to zip all files and folders(with their files and folder). I want to create zip file as like in windows (set all file and folders in .zip file).

Ex:

source_folder
    directory_a/
        a.jpg
        b.jpg
    directory_b/
        x.jpg
        y.jpg
        z.jpg
m.jpg
n.jpg

Make .zip file as defined in souce_folder.

Please help me to solve this problem. Thanks in advance.

Full code:

function Zip($source, $destination)
{    
    if (!extension_loaded('zip') || !file_exists($source)) {     
        return false;     
    }       

    $zip = new ZipArchive();    
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {    
        return false;    
    }    

    $source = str_replace('\\', '/', realpath($source));    

    if (is_dir($source) === true)      
    {      
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);        

        foreach ($files as $file)     
        {      
            $file = str_replace('\\', '/', $file);     

            // Ignore "." and ".." folders        
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )        
                continue;    

            $file = realpath($file);    

            if (is_dir($file) === true)    
            {    
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));    
            }    
            else if (is_file($file) === true)    
            {    
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));      
            }       
        }      
    }    
    else if (is_file($source) === true)    
    {    
        $zip->addFromString(basename($source), file_get_contents($source));    
    }    

    return $zip->close();    
}        

$path = "download/foldername";
Zip($path, $path.".zip");
Community
  • 1
  • 1
Akash
  • 123
  • 1
  • 13
  • What error you are getting, the reference code looks perfect – Dharmesh Patel Aug 13 '14 at 17:30
  • @DharmeshPatel I didn't get any error. but it doesn't creates sub-folder. ex. directory_a and directory_b will not create. it will copy all files into main directory(source_folder). ex. a.jpg, b.jpg, x.jpg, y.jpg, z.jpg, m.jpg, n.jpg into souce_folder – Akash Aug 13 '14 at 17:37
  • if you can add your code that will be easy to debug – Dharmesh Patel Aug 13 '14 at 17:38
  • @DharmeshPatel I changed content. – Akash Aug 13 '14 at 17:59
  • I tested your code locally and it is working here, the only problem I faced is because I tested this on the windows it took fool path starting from c:\... – Dharmesh Patel Aug 13 '14 at 18:22

2 Answers2

0

Use the below code snippet

$zip = new ZipArchive();
$zip->open('path/to/zipfile.zip', ZipArchive::CREATE);
$zip->addFile('a.jpg', 'directory_a/a.jpg');
$zip->addFile('b.jpg', 'b.jpg');
$zip->addFile('x.jpg', 'directory_b/x.jpg');
$zip->addFile('y.jpg', 'y.jpg');
$zip->addFile('z.jpg', 'z.jpg');
$zip->addFile('m.jpg', 'm.jpg');
$zip->addFile('n.jpg', 'n.jpg');

$zip->close();

I assume all the files a.jpg, b.jpg... n.jpg are at same level(at root directory)

Somnath Paul
  • 190
  • 1
  • 6
  • 17
0

Try this,

Replace:

$file = realpath($file);

With:

$file = realpath($file);
$file = str_replace('\\', '/', $file);

If nothing works, here is an alternative function

function Zip($source, $destination)
{    
    if (!extension_loaded('zip') || !file_exists($source)) {     
        return false;     
    }       

    $zip = new ZipArchive();    
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {    
        return false;    
    }    

    if (is_dir($source) === true)      
    {      
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);        

        foreach ($files as $file)     
        {      
            $file = str_replace('\\', '/', $file);     

            // Ignore "." and ".." folders        
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )        
                continue;    

            if (is_dir($file) === true)    
            {    
                $zip->addEmptyDir(str_replace($source . '/', '', $file));    
            }    
            else if (is_file($file) === true)    
            {    
                $zip->addFile($file, str_replace($source . '/', '', $file));      
            }       
        }      
    }
    else if (is_file($source) === true)    
    {    
        $zip->addFromString(basename($source), file_get_contents($source));    
    }    

    return $zip->close();    
}        

$path = "download/somefolder";
Zip($path,$path.'.zip');
Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12