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

📄 features.remote-files.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Using remote files</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.file-upload.put-method.html">PUT method support</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.connection-handling.html">Connection handling</a></div> <div class="up"><a href="features.html">Features</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div>  <h1>Using remote files</h1>  <p class="para">   As long as <span class="option">allow_url_fopen</span> is enabled in   <var class="filename">php.ini</var>, you can use <acronym title="Hypertext Transfer Protocol">HTTP</acronym> and <acronym title="File Transfer Protocol">FTP</acronym>    URLs with most of the functions   that take a filename as a parameter.  In addition, URLs can be   used with the <b>include()</b>,   <b>include_once()</b>, <b>require()</b> and   <b>require_once()</b> statements (since PHP 5.2.0,   <span class="option">allow_url_include</span> must be enabled for these).   See <a href="wrappers.html" class="xref">List of Supported Protocols/Wrappers</a> for more information about the protocols   supported by PHP.  </p>  <blockquote><p><b class="note">Note</b>:       In PHP 4.0.3 and older, in order to use URL wrappers, you were required   to configure PHP using the configure option   <span class="option">--enable-url-fopen-wrapper</span>.   <br />  </p></blockquote>  <p class="para">   <blockquote><p><b class="note">Note</b>:          The Windows versions of PHP earlier than PHP 4.3     did not support remote file accessing for the following functions:     <b>include()</b>, <b>include_once()</b>,     <b>require()</b>, <b>require_once()</b>,     and the imagecreatefromXXX functions in the <a href="ref.image.html" class="xref">GD Functions</a>     extension.    <br />   </p></blockquote>  </p>  <p class="para">   For example, you can use this to open a file on a remote web server,   parse the output for the data you want, and then use that data in a   database query, or simply to output it in a style matching the rest   of your website.  </p>  <p class="para">   <div class="example">    <p><b>Example #1 Getting the title of a remote page</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Unable&nbsp;to&nbsp;open&nbsp;remote&nbsp;file.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br />while&nbsp;(!</span><span style="color: #0000BB">feof&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;This&nbsp;only&nbsp;works&nbsp;if&nbsp;the&nbsp;title&nbsp;and&nbsp;its&nbsp;tags&nbsp;are&nbsp;on&nbsp;one&nbsp;line&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">eregi&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;title&gt;(.*)&lt;/title&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$title&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   You can also write to files on an FTP server (provided that you   have connected as a user with the correct access rights). You   can only create new files using this method; if you try to overwrite   a file that already exists, the <a href="function.fopen.html" class="function">fopen()</a> call will   fail.  </p>  <p class="para">   To connect as a user other than &#039;anonymous&#039;, you need to specify   the username (and possibly password) within the URL, such as   &#039;ftp://user:password@ftp.example.com/path/to/file&#039;. (You can use the   same sort of syntax to access files via HTTP when they require Basic   authentication.)  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Storing data on a remote server</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://ftp.example.com/incoming/outputfile"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Unable&nbsp;to&nbsp;open&nbsp;remote&nbsp;file&nbsp;for&nbsp;writing.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /></span><span style="color: #FF8000">/*&nbsp;Write&nbsp;the&nbsp;data&nbsp;here.&nbsp;*/<br /></span><span style="color: #0000BB">fwrite&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>      </div>   </div>  </p>  <p class="para">   <blockquote><p><b class="note">Note</b>:          You might get the idea from the example above that you can use     this technique to write to a remote log file. Unfortunately     that would not work because the <a href="function.fopen.html" class="function">fopen()</a> call will     fail if the remote file already exists. To do distributed logging     like that, you should take a look at <a href="function.syslog.html" class="function">syslog()</a>.    <br />    </p></blockquote>  </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.file-upload.put-method.html">PUT method support</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.connection-handling.html">Connection handling</a></div> <div class="up"><a href="features.html">Features</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 + -