PHP Doku:: Fügt eine Datei von einem gegebenen Pfad zu einem ZIP-Archiv hinzu - function.ziparchive-addfile.html

Verlauf / Chronik / History: (3) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur Datenkompression und ArchivierungZipThe ZipArchive classZipArchive::addFile

Ein Service von Reinhard Neidl - Webprogrammierung.

The ZipArchive class

<<ZipArchive::addEmptyDir

ZipArchive::addFromString>>

ZipArchive::addFile

(PHP 5 >= 5.2.0, PECL zip >= 1.1.0)

ZipArchive::addFileFügt eine Datei von einem gegebenen Pfad zu einem ZIP-Archiv hinzu

Beschreibung

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

Fügt eine Datei von einem gegebenen Pfad zu einem ZIP-Archiv hinzu.

Parameter-Liste

filename

Der Pfad zur hinzuzufügenden Datei.

localname

Lokaler Name innerhalb des ZIP-Archivs.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Dieses Beispiel öffnet ein ZIP-Dateiarchiv test.zip und fügt die Datei /pfad/zur/datei.txt als neuername.txt hinzu.

Beispiel #1 Öffnen und extrahieren

<?php
$zip 
= new ZipArchive;
if (
$zip->open('test.zip') === TRUE) {
    
$zip->addFile('/pfad/zur/datei.txt''neuername.txt');
    
$zip->close();
    echo 
'ok';
} else {
    echo 
'Fehler';
}
?>

17 BenutzerBeiträge:
- Beiträge aktualisieren...
john factorial
29.12.2010 21:57
Beware: calling $zip->addFile() on a file that doesn't exist will succeed and return TRUE, delaying the failure until you make the final $zip->close() call, which will return FALSE and potentially leave you scratching your head.

If you're adding multiple files to a zip and your $zip->close() call is returning FALSE, ensure that all the files you added actually exist.

It's also a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.
aartdebruijn at gmail dot com
4.11.2010 15:37
When adding a file to your zip, the file is opened and stays open.
When adding over 1024 files (depending on your open files limit) the server stops adding files, resulting in a status 11 in your zip Archive. There is no warning when exceeding this open files limit with addFiles.

Check your open files with ulimit -a

This kept me busy for some time.
camuc at camuc dot net
27.04.2010 17:23
In some versions of this library you NEED to add the "localfile" parameter or the file will not show in the Zip folder.
pelpet at ic dot cz
20.04.2010 18:02
<?php
$zip
=new ZipArchive;
$zip->addFile('path to the file', 'new name of the file');
?>

What I have to do, when I wan't to have the file in the zip archive with it's name before archiving?
(Sorry, I'm from Czech Republic and I can't speak English very well, if I did anywhere mistake, so ignore that pease :-)
jayarjo
24.02.2010 10:44
It is not obvious, since there are no noticeable examples around, but you can use $localname (second parameter) to define and control file/directory structure inside the zip. Use it if you do not want files to be included with their absolute directory tree.

<?php

$zip
->addFile($abs_path, $relative_path);

?>
Anonymous
18.01.2010 19:30
On my system (Windows), I found that ZipArchive uses IBM850 encoding for filenames (localname). For filenames with special characters such as (é) &eacute; which appears at 0xE9 in the ISO-8859-1, it is at 0x82 in IBM850. I had to call iconv('ISO-8859-1', 'IBM850', 'Québec') to get correct file names.
peter at boring dot ch
22.08.2009 15:10
Here's a little extension to ZipArchive that handles directories recursively:

<?php

class Zipper extends ZipArchive {
   
public function
addDir($path) {
    print
'adding ' . $path . '<br>';
   
$this->addEmptyDir($path);
   
$nodes = glob($path . '/*');
    foreach (
$nodes as $node) {
        print
$node . '<br>';
        if (
is_dir($node)) {
           
$this->addDir($node);
        } else if (
is_file($node))  {
           
$this->addFile($node);
        }
    }
}
   
}
// class Zipper

?>
christophe dot braud at aquafadas dot com
11.08.2009 12:10
If you have some warnings with ZipArchiveImproved since the last Ubuntu update, replace "self::CREATE" by "self::CREATE | self::OVERWRITE" in the reopen function

christophe
garcia at no_span dot krautzer-lynn dot com
24.03.2009 15:31
If you add files that have an absolut path, like for example:
/mnt/repository/my_file.pdf
the standard windows zip utility will not be able to extract the files. The first slash trips the zip utility. You have to add relative file paths or use a symbolic link.
ss at littlerain dot com
31.01.2009 1:45
Note that the file isn't actually added to the archive until the $zip->close() method is called. I spent a lot of time trying to figure out why no time() went by after adding large files via $zip->addFile() but would then time out the script.
Farzad Ghanei
18.01.2009 11:53
here is a basic class that extends the ZipArchive to:
  * add a functionality to report the ZIP file address (I needed it and I could not find out how in ZipArchive documentation).
  * resolve the problem of adding so many files to the archive due file descriptor limit. the ZipArchiveImproved::addFile() handles this.

<?php
/**
 * ZipArchiveImproved extends ZipArchive to add some information about the zip file and some functionality.
 *
 *
 *
 * @author Farzad Ghanei
 * @uses ZipArchive
 * @version 1.0.0 2009-01-18
 */

class ZipArchiveImproved extends ZipArchive {
    protected
$_archiveFileName = null;
    protected
$_newAddedFilesCounter = 0;
    protected
$_newAddedFilesSize = 100;
   
   
/**
     * returns the name of the archive file.
     *
     * @return string
     */
   
public function getArchiveFileName() {
        return
$this->_archiveFileName;
    }

   
/**
     * returns the number of files that are going to be added to ZIP
     * without reopenning the stream to file.
     *
     * @return int
     */
   
public function getNewAddedFilesSize() {
        return
$this->_newAddedFilesSize;
    }

   
/**
     * sets the number of files that are going to be added to ZIP
     * without reopenning the stream to file. if no size is specified, default is 100.
     *
     * @param int
     * @return ZipArchiveImproved self reference
     */
   
public function setNewlAddedFilesSize($size=100) {
        if ( empty(
$size) || !is_int($size) || $size < 1) {
           
$size = 100;
        }
       
$this->_newAddedFilesSize = $size;
        return
$this;
    }

   
/**
     * opens a stream to a ZIP archive file. calls the ZipArchive::open() internally.
     * overwrites ZipArchive::open() to add the archiveFileName functionality.
     *
     * @param string $fileName
     * @param int $flags
     * return mixed
     */
   
public function open($fileName, $flags) {
       
$this->_archiveFileName = $fileName;
       
$this->_newAddedFilesCounter = 0;
        return
parent::open($fileName,$flags);
    }

   
/**
     * closes the stream to ZIP archive file. calls the ZipArchive::close() internally.
     * overwrites ZipArchive::close() to add the archiveFileName functionality.
     *
     * @return bool
     */
   
public function close() {
       
$this->_archiveFileName = null;
       
$this->_newAddedFilesCounter = 0;
        return
parent::close();
    }

   
/**
     * closes the connection to ZIP file and openes the connection again.
     *
     * @return bool
     */
   
public function reopen() {
       
$archiveFileName = $this->_archiveFileName;
        if ( !
$this->close() ) {
            return
false;
        }
        return
$this->open($archiveFileName,self::CREATE);
    }

   
/**
     * adds a file to a ZIP archive from the given path. calls the ZipArchive::addFile() internally.
     * overwrites ZipArchive::addFile() to handle maximum file connections in operating systems.
     *
     * @param string $fileName the path to file to be added to archive
     * @param string [optional] $localname the name of the file in the ZIP archive
     * @return bool
     */
   
public function addFile( $fileName ) {
        if (
$this->_newAddedFilesCounter >= $this->_newAddedFilesSize) {
           
$this->reopen();
        }
        if (
func_num_args() > 1 ) {
           
$flags = func_get_arg(1);
           
$added = parent::addFile($fileName,$flags);
            if (
$added) {
               
$this->_newAddedFilesCounter++;
            }
            return
$added;
        }
       
$added = parent::addFile($fileName);
        if (
$added) {
           
$this->_newAddedFilesCounter++;
        }
        return
$added;
    }
// public function addFile()
}
?>
marco at maranao dot ca
15.12.2008 19:45
This is my workaround for the file descriptor limit by closing/opening the archive file periodically.

<?php
if($backup = new ZipArchive()) {
    if(
$backup->open($zip, ZIPARCHIVE::OVERWRITE) === true) {
       
$backup->addFile($file['realpath'], $file['path']);
        if((
$count++) == 200) { // the file descriptor limit
           
$backup->close();
            if(
$backup = new ZipArchive()) {
               
$backup->open($zip);
               
$count = 0;
            }
        }
    }
   
$backup->close();
}
?>

Hope it helps someone.
javierseixas at gmail dt com
17.07.2008 17:30
I have had several problems trying adding files, because of a path problem. The error gived was this:

ZipArchive::addFile() [function.ZipArchive-addFile]: Unable to access <path>

I used an absolute root starting by "/", and it didn't work. Try starting your path with "./" (referencing the root of your site).
wacher at freemail dot hu
23.06.2008 22:03
The workaround above (file_get_contents) is very dangerous if you pack large files. (see memory limit).
Close/open the zip archive periodically instead of using file_get_contents().
stanleyshilov {} gmail.com
11.05.2008 21:05
It should be noted that the example provided above is not accurate.

Unlike extractTo, zip_open does not return a boolean result, so the above example will always fail.
mike at thetroubleshooters dot dk
7.02.2008 16:20
What is worse is that when you run out of filedescriptors it seems to fail silently, I have not been able to find any errors in any logfiles.
Andreas R. newsgroups2005 at geekmail de
4.04.2007 3:29
Currently the number of files that can be added using addFile to the ZIP archive (until it is closed) is limited by file descriptors limit. This is an easy workaround (on the bug links below you can find another workarounds):
<?php
   
/** work around file descriptor number limitation (to avoid failure
     * upon adding more than typically 253 or 1024 files to ZIP) */
   
function addFileToZip( $zip, $path, $zipEntryName ) {
       
// this would fail with status ZIPARCHIVE::ER_OPEN
        // after certain number of files is added since
        // ZipArchive internally stores the file descriptors of all the
        // added files and only on close writes the contents to the ZIP file
        // see: http://bugs.php.net/bug.php?id=40494
        // and: http://pecl.php.net/bugs/bug.php?id=9443
        // return $zip->addFile( $path, $zipEntryName );

       
$contents = file_get_contents( $path );
        if (
$contents === false ) {
            return
false;
        }
        return
$zip->addFromString( $zipEntryName, $contents );
    }
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)