<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Adds a file to a ZIP archive from the given path</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ziparchive.addemptydir.html">ZipArchive::addEmptyDir</a></div> <div class="next" style="text-align: right; float: right;"><a href="ziparchive.addfromstring.html">ZipArchive::addFromString</a></div> <div class="up"><a href="class.ziparchive.html">ZipArchive</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="ziparchive.addfile" class="refentry"> <div class="refnamediv"> <h1 class="refname">ZipArchive::addFile</h1> <p class="verinfo">(PHP 5 >= 5.2.0, PHP 7, PECL zip >= 1.1.0)</p><p class="refpurpose"><span class="refname">ZipArchive::addFile</span> — <span class="dc-title">Adds a file to a ZIP archive from the given path</span></p> </div> <div class="refsect1 description" id="refsect1-ziparchive.addfile-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>ZipArchive::addFile</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$localname</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$start</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = 0</span></span> ]]] )</div> <p class="para rdfs-comment"> Adds a file to a ZIP archive from a given path. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">For maximum portability, it is recommended to always use forward slashes (<em>/</em>) as directory separator in ZIP filenames.</span></p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-ziparchive.addfile-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">filename</code></dt> <dd> <p class="para"> The path to the file to add. </p> </dd> <dt> <code class="parameter">localname</code></dt> <dd> <p class="para"> If supplied, this is the local name inside the ZIP archive that will override the <code class="parameter">filename</code>. </p> </dd> <dt> <code class="parameter">start</code></dt> <dd> <p class="para"> This parameter is not used but is required to extend <a href="class.ziparchive.html" class="classname">ZipArchive</a>. </p> </dd> <dt> <code class="parameter">length</code></dt> <dd> <p class="para"> This parameter is not used but is required to extend <a href="class.ziparchive.html" class="classname">ZipArchive</a>. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-ziparchive.addfile-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-ziparchive.addfile-examples"> <h3 class="title">Examples</h3> <p class="para"> This example opens a ZIP file archive <var class="filename">test.zip</var> and add the file <var class="filename">/path/to/index.txt</var>. as <var class="filename">newname.txt</var>. </p> <div class="example" id="example-865"> <p><strong>Example #1 Open and add</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$zip </span><span style="color: #007700">= new </span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-></span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.zip'</span><span style="color: #007700">) === </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$zip</span><span style="color: #007700">-></span><span style="color: #0000BB">addFile</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/index.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'newname.txt'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$zip</span><span style="color: #007700">-></span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /> echo </span><span style="color: #DD0000">'ok'</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">'failed'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 notes" id="refsect1-ziparchive.addfile-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> When a file is set to be added to the archive, PHP will lock the file. The lock is only released once the <a href="class.ziparchive.html" class="classname">ZipArchive</a> object has been closed, either via <span class="methodname"><a href="ziparchive.close.html" class="methodname">ZipArchive::close()</a></span> or the <a href="class.ziparchive.html" class="classname">ZipArchive</a> object being destroyed. This may prevent you from being able to delete the file being added until after the lock has been released. </p> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ziparchive.addemptydir.html">ZipArchive::addEmptyDir</a></div> <div class="next" style="text-align: right; float: right;"><a href="ziparchive.addfromstring.html">ZipArchive::addFromString</a></div> <div class="up"><a href="class.ziparchive.html">ZipArchive</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>