<!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>Using remote files</title> </head> <body><div class="manualnavbar" 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 id="features.remote-files" class="chapter"> <h1>Using remote files</h1> <p class="para"> As long as <strong class="option unknown">allow_url_fopen</strong> 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 <span class="function"><a href="function.include.html" class="function">include</a></span>, <span class="function"><a href="function.include-once.html" class="function">include_once</a></span>, <span class="function"><a href="function.require.html" class="function">require</a></span> and <span class="function"><a href="function.require-once.html" class="function">require_once</a></span> statements (since PHP 5.2.0, <strong class="option unknown">allow_url_include</strong> must be enabled for these). See <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for more information about the protocols supported by PHP. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> In PHP 4.0.3 and older, in order to use URL wrappers, you were required to configure PHP using the configure option <strong class="option unknown">--enable-url-fopen-wrapper</strong> . </p> </p></blockquote> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The Windows versions of PHP earlier than PHP 4.3 did not support remote file accessing for the following functions: <span class="function"><a href="function.include.html" class="function">include</a></span>, <span class="function"><a href="function.include-once.html" class="function">include_once</a></span>, <span class="function"><a href="function.require.html" class="function">require</a></span>, <span class="function"><a href="function.require-once.html" class="function">require_once</a></span>, and the imagecreatefromXXX functions in the <a href="ref.image.html" class="xref">GD and Image Functions</a> extension. </p> </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" id="example-361"> <p><strong>Example #1 Getting the title of a remote page</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen </span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$file</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"<p>Unable to open remote file.\n"</span><span style="color: #007700">;<br /> exit;<br />}<br />while (!</span><span style="color: #0000BB">feof </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/* This only works if the title and its tags are on one line */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">preg_match </span><span style="color: #007700">(</span><span style="color: #DD0000">"@\<title\>(.*)\</title\>@i"</span><span style="color: #007700">, </span><span style="color: #0000BB">$line</span><span style="color: #007700">, </span><span style="color: #0000BB">$out</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$title </span><span style="color: #007700">= </span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /> break;<br /> }<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">?></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 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> call will fail. </p> <p class="para"> To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as '<em>ftp://user:password@ftp.example.com/path/to/file</em>'. (You can use the same sort of syntax to access files via <acronym title="Hypertext Transfer Protocol">HTTP</acronym> when they require Basic authentication.) </p> <p class="para"> <div class="example" id="example-362"> <p><strong>Example #2 Storing data on a remote server</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen </span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://ftp.example.com/incoming/outputfile"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$file</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"<p>Unable to open remote file for writing.\n"</span><span style="color: #007700">;<br /> exit;<br />}<br /></span><span style="color: #FF8000">/* Write the data here. */<br /></span><span style="color: #0000BB">fwrite </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<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">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> call will fail if the remote file already exists. To do distributed logging like that, you should take a look at <span class="function"><a href="function.syslog.html" class="function">syslog()</a></span>. </p> </p></blockquote> </p> </div> <hr /><div class="manualnavbar" 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>