Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.14     Remote files

This is NOT the latest copy of this book; click here for the latest version.

As you have seen, fopen() is a very helpful function - it allows you to manipulate any files for which you have permission. However, its usefulness is only just beginning - fopen() is even more helpful than you have seen so far!

The key lies in the first parameter to fopen() - rather than using local file names, you can specify remote files also - even files stored on HTTP and FTP servers. PHP automatically opens a HTTP/FTP connection for you, returning the file handle as usual. For all intents and purposes, a file handle returned from a remote file is good for all the same uses as a local file handle. Let's take a look at an example of reading a remote file through fopen():

<?php
    $slash
= fopen("http://www.slashdot.org", "r");
    
$site = fread($slash, 200000);
    
fclose($slash);
    print
$site;
?>

Try saving the above code into a file, "openremote.php", and loading it into your web browser - you should see Slashdot displayed. What happens behind the scenes is that when your PHP script executes, it opens a connection the Slashdot server, downloads the default web page, and then transfers it to you as if it were part of your site. Notice how the "r" mode is specified - web servers do not allow writing through HTTP (without WebDAV), and some will even deny access for reading if you are an anonymous visitor, as PHP normally is.

Over FTP, however, you are able to read and write, so you are able to upload and download files freely using fopen() and an FTP connection.

Author's Note: If you are looking to find a quick way to execute an external script, you should try using fopen(). For example, to call foo.php on example.com, use fopen("www.example.com/foo.php", "r"). You need not bother reading in the results - simply opening the connection is enough to make the server on example.com process the contents of foo.php.





<< 8.13.2 One last directory function: scandir()   8.15 File checksums: sha1_file() and md5_file() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Guna - 29 Aug 2008

In php ini setting , there is a directive called allow_url_fopen. Is this anyway related to the current topic - "remote file accessing"?

Jim Plush - 29 Aug 2008

I've found that using the PHP.ini setting

; Define the User-Agent string
; user_agent="PHP"

works well for sites that try and block anonymous reads. You should set this to a valid agent that a regular user would have.

something like

user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041210 Firefox/1.0"



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow