⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phar.using.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
 <p class="para">  <div class="informalexample">   <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;open&nbsp;an&nbsp;existing&nbsp;phar<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'coollibrary.phar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Phar&nbsp;extends&nbsp;SPL's&nbsp;DirectoryIterator&nbsp;class<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">foreach&nbsp;(new&nbsp;</span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$p</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$file&nbsp;is&nbsp;a&nbsp;PharFileInfo&nbsp;class,&nbsp;and&nbsp;inherits&nbsp;from&nbsp;SplFileInfo<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getFileName</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPathName</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;display&nbsp;contents;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'internal/file.php'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'internal/file.php'</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getMetaData</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;create&nbsp;a&nbsp;new&nbsp;phar&nbsp;-&nbsp;phar.readonly&nbsp;must&nbsp;be&nbsp;0&nbsp;in&nbsp;php.ini<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;phar.readonly&nbsp;is&nbsp;enabled&nbsp;by&nbsp;default&nbsp;for&nbsp;security&nbsp;reasons.<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;On&nbsp;production&nbsp;servers,&nbsp;Phars&nbsp;need&nbsp;never&nbsp;be&nbsp;created,<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;only&nbsp;executed.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">canWrite</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'newphar.tar.phar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'newphar.tar.phar'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;make&nbsp;this&nbsp;a&nbsp;tar-based&nbsp;phar&nbsp;archive,&nbsp;compressed&nbsp;with&nbsp;gzip&nbsp;compression&nbsp;(.tar.gz)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">convertToExecutable</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">TAR</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">GZ</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;create&nbsp;transaction&nbsp;-&nbsp;nothing&nbsp;is&nbsp;written&nbsp;to&nbsp;newphar.phar<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;until&nbsp;stopBuffering()&nbsp;is&nbsp;called,&nbsp;although&nbsp;temporary&nbsp;storage&nbsp;is&nbsp;needed<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">startBuffering</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;add&nbsp;all&nbsp;files&nbsp;in&nbsp;/path/to/project,&nbsp;saving&nbsp;in&nbsp;the&nbsp;phar&nbsp;with&nbsp;the&nbsp;prefix&nbsp;"project"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">DirectoryIterator</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/project'</span><span style="color: #007700">)),&nbsp;</span><span style="color: #DD0000">'/path/to/'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;add&nbsp;a&nbsp;new&nbsp;file&nbsp;via&nbsp;the&nbsp;array&nbsp;access&nbsp;API<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'file1.txt'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'Information'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'hugefile.dat'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;copy&nbsp;all&nbsp;data&nbsp;from&nbsp;the&nbsp;stream<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'data/hugefile.dat'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$fp</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">canCompress</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">GZ</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'data/hugefile.dat'</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">compress</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">GZ</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'images/wow.jpg'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'images/wow.jpg'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;any&nbsp;value&nbsp;can&nbsp;be&nbsp;saved&nbsp;as&nbsp;file-specific&nbsp;meta-data<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'images/wow.jpg'</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">setMetaData</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'mime-type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'image/jpeg'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">[</span><span style="color: #DD0000">'index.php'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.php'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setMetaData</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'bootstrap'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'index.php'</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;save&nbsp;the&nbsp;phar&nbsp;archive&nbsp;to&nbsp;disk<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stopBuffering</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Could&nbsp;not&nbsp;open&nbsp;Phar:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>   </div>  </div> </p> <p class="para">  In addition, verification of phar file contents can be done using any of the  supported symmetric hash algorithms (MD5, SHA1, SHA256 and SHA512 if ext/hash is enabled)  and using asymmetric public/private key signing using OpenSSL (new in Phar 2.0.0).  To  take advantage of OpenSSL signing, you need to generate a public/private key pair, and  use the private key to set the signature using  <a href="phardata.setsignaturealgorithm.html" class="function">Phar::setSignatureAlgorithm()</a>.  In addition, the public key  as extracted using this code:  <div class="example-contents">   <div class="phpcode"><code><span style="color: #000000">$public&nbsp;=&nbsp;openssl_get_publickey(file_get_contents('private.pem'));<br />$pkey&nbsp;=&nbsp;'';<br />openssl_pkey_export($public,&nbsp;$pkey);</span></code></div>  </div>  must be saved adjacent to the phar archive it verifies.  If the phar archive  is saved as <i>/path/to/my.phar</i>, the public key must be saved  as <i>/path/to/my.phar.pubkey</i>, or phar will be unable to verify  the OpenSSL signature. </p> <p class="para">  As of version 2.0.0, The <a href="class.Phar.html" class="classname">Phar</a> class also provides 3 static methods, <a href="phar.webphar.html" class="function">Phar::webPhar()</a>,  <a href="phar.mungserver.html" class="function">Phar::mungServer()</a> and <a href="phar.interceptfilefuncs.html" class="function">Phar::interceptFileFuncs()</a> that are crucial  to packaging up PHP applications designed for usage on regular filesystems and for web-based applications.  <a href="phar.webphar.html" class="function">Phar::webPhar()</a> implements a front controller that routes HTTP calls to the correct  location within the phar archive.  <a href="phar.mungserver.html" class="function">Phar::mungServer()</a> is used to modify the values of  the <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var> array to trick applications that process these values.  <a href="phar.interceptfilefuncs.html" class="function">Phar::interceptFileFuncs()</a> instructs Phar to intercept calls to  <a href="function.fopen.html" class="function">fopen()</a>, <a href="function.file-get-contents.html" class="function">file_get_contents()</a>, <a href="function.opendir.html" class="function">opendir()</a>, and  all of the stat-based functions (<a href="function.file-exists.html" class="function">file_exists()</a>, <a href="function.is-readable.html" class="function">is_readable()</a> and so on) and  route all relative paths to locations within the phar archive. </p> <p class="para">  As an example, packaging up a release of the popular phpMyAdmin application for use as a phar archive requires  only this simple script and then <i>phpMyAdmin.phar.tar.php</i> can be accessed as a regular file  from your web server after modifying the user/password: </p> <p class="para">  <div class="informalexample">   <div class="example-contents">    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">@</span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #DD0000">'phpMyAdmin.phar.tar.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">copy</span><span style="color: #007700">(</span><span style="color: #DD0000">'phpMyAdmin-2.11.3-english.tar.gz'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'phpMyAdmin.phar.tar.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'phpMyAdmin.phar.tar.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">startBuffering</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">"phpMyAdmin-2.11.3-english/config.inc.php"</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'&lt;?php<br />/*&nbsp;Servers&nbsp;configuration&nbsp;*/<br />$i&nbsp;=&nbsp;0;<br /><br />/*&nbsp;Server&nbsp;localhost&nbsp;(config:root)&nbsp;[1]&nbsp;*/<br />$i++;<br />$cfg[\'Servers\'][$i][\'host\']&nbsp;=&nbsp;\'localhost\';<br />$cfg[\'Servers\'][$i][\'extension\']&nbsp;=&nbsp;\'mysqli\';<br />$cfg[\'Servers\'][$i][\'connect_type\']&nbsp;=&nbsp;\'tcp\';<br />$cfg[\'Servers\'][$i][\'compress\']&nbsp;=&nbsp;false;<br />$cfg[\'Servers\'][$i][\'auth_type\']&nbsp;=&nbsp;\'config\';<br />$cfg[\'Servers\'][$i][\'user\']&nbsp;=&nbsp;\'root\';<br />$cfg[\'Servers\'][$i][\'password\']&nbsp;=&nbsp;\'\';<br /><br /><br />/*&nbsp;End&nbsp;of&nbsp;servers&nbsp;configuration&nbsp;*/<br />if&nbsp;(strpos(PHP_OS,&nbsp;\'WIN\')&nbsp;!==&nbsp;false)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;$cfg[\'UploadDir\']&nbsp;=&nbsp;getcwd();<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;$cfg[\'UploadDir\']&nbsp;=&nbsp;\'/tmp/pharphpmyadmin\';<br />&nbsp;&nbsp;&nbsp;&nbsp;@mkdir(\'/tmp/pharphpmyadmin\');<br />&nbsp;&nbsp;&nbsp;&nbsp;@chmod(\'/tmp/pharphpmyadmin\',&nbsp;0777);<br />}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;?php<br />Phar::interceptFileFuncs();<br />Phar::webPhar("phpMyAdmin.phar",&nbsp;"phpMyAdmin-2.11.3-english/index.php");<br />echo&nbsp;"phpMyAdmin&nbsp;is&nbsp;intended&nbsp;to&nbsp;be&nbsp;executed&nbsp;from&nbsp;a&nbsp;web&nbsp;browser\n";<br />exit&nbsp;-1;<br />__HALT_COMPILER();<br />'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stopBuffering</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>   </div>  </div> </p></div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.using.stream.html">Using Phar Archives: the phar stream wrapper</a></div> <div class="up"><a href="book.phar.html">Phar</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -