Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Remote Files

The fopen() function allows you to manipulate any files for which you have permission. However, its usefulness is only just beginning, because you can specify remote files as well as local files—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.

This example displays the Slashdot web site through your browser:

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

The r mode is specified because 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.

Tip

If you are looking to find a quick way to execute an external script, 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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page