Asked  7 Months ago    Answers:  5   Viewed   35 times

I'm trying to use php to create a zip file (which it does - taken from this page -, however inside the zip file are all of the folder names to the file itself.

Is it possible to just have the file inside the zip minus all the folders?

Here's my code:

function create_zip($files = array(), $destination = '', $overwrite = true) {

    if(file_exists($destination) && !$overwrite) { return false; };
    $valid_files = array();
    if(is_array($files)) {
        foreach($files as $file) { 
            if(file_exists($file)) { 
                $valid_files[] = $file;
    if(count($valid_files)) { 
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
            return false;
        foreach($valid_files as $file) { 
        return file_exists($destination);
    } else {
        return false;


$files_to_zip = array('/media/138/file_01.jpg','/media/138/file_01.jpg','/media/138/file_01.jpg');

$result = create_zip($files_to_zip,'/...full_site_path.../downloads/138/');



The problem here is that $zip->addFile is being passed the same two parameters.

According to the documentation:

bool ZipArchive::addFile ( string $filename [, string $localname ] )

The path to the file to add.

local name inside ZIP archive.

This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

When you supply the second parameter, you'll want to strip the path from it when adding it to the zip archive. For example, on Unix-based systems this would look like:

$new_filename = substr($file,strrpos($file,'/') + 1);
Wednesday, March 31, 2021
answered 7 Months ago

this will ensure a file with .php extension will not be added:

   foreach ($files as $file) {
        if(!strstr($file,'.php')) $zip->addFile($file);

edit: here's the full code rewritten:


    $zipname = '';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    if ($handle = opendir('.')) {
      while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {


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

Wednesday, March 31, 2021
answered 7 Months ago

Any ideas how to stop this.

The error message tells you how to stop this.

You are required to use the date.timezone setting or the date_default_timezone_set() function.

Update your PHP installation's php.ini (likely /etc/php.ini) file to have a date.timezone setting, or use date_default_timezone_set() in your scripts.

You're only now seeing this because it starts happening in PHP 5.3+.

Wednesday, March 31, 2021
answered 7 Months ago

This doesn't exist, but it could easily exist in the current framework. Just as git acts differently with displaying binary or ascii files when performing a diff, it could be told to offer special treatment to certain file types through the configuration interface.

If you don't want to change the code base (although this is kind of a cool idea you've got), you could also script it for yourself by using pre-commit and post-checkout hooks to unzip and store the files, then return them to their .zip state on checkout. You would have to restrict actions to only those files blobs / indexes that are specified by git add.

Either way is a bit of work -- it's just a question of whether the other git commands are aware of what's going on and play nicely.

Friday, July 9, 2021
answered 4 Months ago

Certain spreadsheet file formats, such as OfficeOpenXML used as the default format by Excel 2007 and above, require ZipArchive. In most recent versions of PHP, ZipArchive is always available, but prior to PHP 5.3 it needed to be manually installed as a PHP extension.

As @briosheje says If you don't have ZipArchive installed/enabled for your PHP, then you can use


PCLZip is included in the PHPExcel distribution as an alternative to PHP's built-in ZipArchive class, though it is quite a bit slower. Enabling PCLZip allows you to work with zip-based spreadsheet formats even without ZipArchive.

However, when we first bundled PCLZip (originally in PHPExcel 1.7.6), it was only an option when writing zip-based formats, not when reading them. We changed that in PHPExcel 1.8.0, so enabling PCLZip now allows you to read zip-based formats as well as writing them.

The PHPEXcel Reader documentation details the different spreadsheet formats, and explains which ones are zip-based.

Monday, July 19, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :