<!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>Copy a file internal to the phar archive to another new file within the phar</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.converttoexecutable.html">Phar::convertToExecutable</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.count.html">Phar::count</a></div> <div class="up"><a href="class.phar.html">Phar</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="phar.copy" class="refentry"> <div class="refnamediv"> <h1 class="refname">Phar::copy</h1> <p class="verinfo">(PHP >= 5.3.0, PECL phar >= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::copy</span> — <span class="dc-title">Copy a file internal to the phar archive to another new file within the phar</span></p> </div> <div class="refsect1 description" id="refsect1-phar.copy-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>Phar::copy</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$oldfile</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$newfile</code></span> )</div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">This method requires the <var class="filename">php.ini</var> setting <em>phar.readonly</em> to be set to <em>0</em> in order to work for <a href="class.phar.html" class="classname">Phar</a> objects. Otherwise, a <a href="class.pharexception.html" class="classname">PharException</a> will be thrown.</p></p></blockquote> <p class="para"> Copy a file internal to the phar archive to another new file within the phar. This is an object-oriented alternative to using <span class="function"><a href="function.copy.html" class="function">copy()</a></span> with the phar stream wrapper. </p> </div> <div class="refsect1 parameters" id="refsect1-phar.copy-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">oldfile</code></em></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">newfile</code></em></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-phar.copy-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> returns <strong><code>TRUE</code></strong> on success, but it is safer to encase method call in a try/catch block and assume success if no exception is thrown. </p> </div> <div class="refsect1 errors" id="refsect1-phar.copy-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> throws <a href="class.unexpectedvalueexception.html" class="classname">UnexpectedValueException</a> if the source file does not exist, the destination file already exists, write access is disabled, opening either file fails, reading the source file fails, or a <a href="class.pharexception.html" class="classname">PharException</a> if writing the changes to the phar fails. </p> </div> <div class="refsect1 examples" id="refsect1-phar.copy-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-672"> <p><strong>Example #1 A <span class="function"><strong>Phar::copy()</strong></span> example</strong></p> <div class="example-contents"><p> This example shows using <span class="function"><strong>Phar::copy()</strong></span> and the equivalent stream wrapper performance of the same thing. The primary difference between the two approaches is error handling. All Phar methods throw exceptions, whereas the stream wrapper uses <span class="function"><a href="function.trigger-error.html" class="function">trigger_error()</a></span>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">try {<br /> </span><span style="color: #0000BB">$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'myphar.phar'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$phar</span><span style="color: #007700">[</span><span style="color: #DD0000">'a'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'hi'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$phar</span><span style="color: #007700">-></span><span style="color: #0000BB">copy</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">);<br /> echo </span><span style="color: #0000BB">$phar</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// outputs "hi"<br /></span><span style="color: #007700">} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// handle error<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// the stream wrapper equivalent of the above code.<br />// E_WARNINGS are triggered on error rather than exceptions.<br /></span><span style="color: #0000BB">copy</span><span style="color: #007700">(</span><span style="color: #DD0000">'phar://myphar.phar/a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'phar//myphar.phar/c'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'phar://myphar.phar/c'</span><span style="color: #007700">); </span><span style="color: #FF8000">// outputs "hi"<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.converttoexecutable.html">Phar::convertToExecutable</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.count.html">Phar::count</a></div> <div class="up"><a href="class.phar.html">Phar</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>