Source of: /manual/en/features.remote-files.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'features.remote-files.php',
1 => 'Using remote files',
),
'up' =>
array (
0 => 'features.php',
1 => 'Features',
),
'prev' =>
array (
0 => 'features.file-upload.put-method.php',
1 => 'PUT method support',
),
'next' =>
array (
0 => 'features.connection-handling.php',
1 => 'Connection handling',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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 <a href="function.include.php" class="function">include()</a>,
<a href="function.include-once.php" class="function">include_once()</a>, <a href="function.require.php" class="function">require()</a> and
<a href="function.require-once.php" class="function">require_once()</a> statements (since PHP 5.2.0,
<span class="option">allow_url_include</span> must be enabled for these).
See <a href="wrappers.php" 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">
</p><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:
<a href="function.include.php" class="function">include()</a>, <a href="function.include-once.php" class="function">include_once()</a>,
<a href="function.require.php" class="function">require()</a>, <a href="function.require-once.php" class="function">require_once()</a>,
and the imagecreatefromXXX functions in the <a href="ref.image.php" class="xref">GD and Image Functions</a>
extension.
<br />
</p></blockquote><p>
</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">
</p><div class="example">
<p><b>Example #1 Getting the title of a remote page</b></p>
<div class="example-contents programlisting">
<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>
<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.php" class="function">fopen()</a> 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
'<i>ftp://user:password@ftp.example.com/path/to/file</i>'.
(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">
</p><div class="example">
<p><b>Example #2 Storing data on a remote server</b></p>
<div class="example-contents programlisting">
<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>
<p class="para">
</p><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.php" 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.php" class="function">syslog()</a>.
<br />
</p></blockquote><p>
</p>
</div>
<?php manual_footer(); ?>