Skip to main content

PHP Features - Using Remote Files

Using remote files ¶

As long as allow_url_fopen is enabled in php.ini, you can use HTTP and FTP URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the includeinclude_oncerequire andrequire_once statements (since PHP 5.2.0, allow_url_include must be enabled for these). See Supported Protocols and Wrappers for more information about the protocols supported by PHP.
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.
Example #1 Getting the title of a remote page
$file fopen ("http://www.example.com/""r");
if (!
$file) {
    echo 
"Unable to open remote file.\n";
    exit;
}
while (!
feof ($file)) {
    
$line fgets ($file1024);
    
/* This only works if the title and its tags are on one line */
    
if (preg_match ("@\(.*)\</title\>@i"</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$line</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$out</span><span style="color: rgb(0, 119, 0);">)) {<br>        </span><span style="color: rgb(0, 0, 187);">$title </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$out</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">];<br>        break;<br>    }<br>}<br></span><span style="color: rgb(0, 0, 187);">fclose</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$file</span><span style="color: rgb(0, 119, 0);">);<br></span><span style="color: rgb(0, 0, 187);">?></span></span></code></div> </div> </div> <p class="para" style="margin-bottom: 1.5rem; color: rgb(51, 51, 51); font-family: "Fira Sans", "Source Sans Pro", Helvetica, Arial, sans-serif; font-size: 16px; background-color: rgb(242, 242, 242);"> 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="http://php.net/manual/en/function.fopen.php" class="function" style="border-bottom: 1px solid; text-decoration-line: none; color: rgb(51, 102, 153);">fopen()</a></span> call will fail.</p> <p class="para" style="margin-bottom: 1.5rem; color: rgb(51, 51, 51); font-family: "Fira Sans", "Source Sans Pro", Helvetica, Arial, sans-serif; font-size: 16px; background-color: rgb(242, 242, 242);"> To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as '<em style="text-rendering: optimizeLegibility;">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" style="margin-bottom: 1.5rem; color: rgb(51, 51, 51); font-family: "Fira Sans", "Source Sans Pro", Helvetica, Arial, sans-serif; font-size: 16px; background-color: rgb(242, 242, 242);"> </p> <div class="example" id="example-389" style="margin: 1.5rem 0px; color: rgb(51, 51, 51); font-family: "Fira Sans", "Source Sans Pro", Helvetica, Arial, sans-serif; font-size: 16px; background-color: rgb(242, 242, 242);"> <p style="margin-bottom: 1.5rem;"> <span style="text-rendering: optimizeLegibility;">Example #2 Storing data on a remote server</span></p> <div class="example-contents" style="margin-bottom: 1.5rem;"> <div class="phpcode" style="background-color: rgb(255, 255, 255); box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 0px 1px inset; border-radius: 0px 0px 2px 2px; padding: 0.75rem;"> <code style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 0.875rem; line-height: 1.5rem; font-family: "Fira Mono", "Source Code Pro", monospace; word-wrap: break-word; display: block;"><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);"><?php<br>$file </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">fopen </span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">"ftp://ftp.example.com/incoming/outputfile"</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">"w"</span><span style="color: rgb(0, 119, 0);">);<br>if (!</span><span style="color: rgb(0, 0, 187);">$file</span><span style="color: rgb(0, 119, 0);">) {<br>    echo </span><span style="color: rgb(221, 0, 0);">"<p> Unable to open remote file for writing.\n"</span><span style="color: rgb(0, 119, 0);">;<br>    exit;<br>}<br></span><span style="color: rgb(255, 128, 0);">/* Write the data here. */<br></span><span style="color: rgb(0, 0, 187);">fwrite </span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$file</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'HTTP_USER_AGENT'</span><span style="color: rgb(0, 119, 0);">] . </span><span style="color: rgb(221, 0, 0);">"\n"</span><span style="color: rgb(0, 119, 0);">);<br></span><span style="color: rgb(0, 0, 187);">fclose </span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$file</span><span style="color: rgb(0, 119, 0);">);<br></span><span style="color: rgb(0, 0, 187);">?></span></span></code></div> </div> </div> </div>

Comments