Programming Tutorials

Opening a Remote File in PHP

By: David Sklar in PHP Tutorials on 2008-12-01  

Pass the file's URL to fopen():

$fh = fopen('http://www.example.com/robots.txt','r') or die($php_errormsg);

When fopen() is passed a filename that begins with http://, it retrieves the given page with an HTTP/1.0 GET request (although a Host: header is also passed along to deal with virtual hosts). Only the body of the reply can be accessed using the file handle, not the headers. Files can be read, not written, via HTTP.

When fopen() is passed a filename that begins with ftp://, it returns a pointer to the specified file, obtained via passive mode FTP. You can open files via FTP for either reading or writing, but not both.

To open URLs that require a username and a password with fopen( ), embed the authentication information in the URL like this:

$fh = fopen('ftp://username:[email protected]/pub/Index','r');
$fh = fopen('http://username:[email protected]/robots.txt','r');

Opening remote files with fopen() is implemented via a PHP feature called the URL fopen wrapper. It's enabled by default but is disabled by setting allow_url_fopen to off in your php.ini or web server configuration file. If you can't open remote files with fopen( ), check your server configuration.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

__autoload() METHOD in PHP

Opening a Remote File in PHP

Installing PHP with Apache 2.x on HP UX 11i and configuring PHP with Oracle 9i

Building PHP with Apache2 on SuSE Professional 9.1/9.2

Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Installing PHP with nginx-server under windows

Warning: session_start(): open .... failed - PHP error

Convert IP address to integer and back to IP address in PHP

Function to force strict boolean values in PHP

Function to return number of digits of an integer in PHP

Function to sort array by elements and count of element in PHP

Floating point precision in PHP

Latest Articles (in PHP)