PHP: Open Remote File

By remote file, I mean files from another website or web server identified by a remote URL.

Usually, you can open such a file via the normal procedures you would take to open a local file with php file functions such as fopen() or file_get_contents(). However, to make this work, you will first make sure the value of allow_url_fopen in php.ini is set to on, otherwise it’d impossible to open a remote file just as you would do with a local one.

To open and possibly read a remotely hosted file in PHP:

$fh = fopen ('http://www.example.com/robots.txt', 'r') or die('Unable to open remote file.');

If allow_url_fopen is set to true, $fh will be a file handler which you can use to read the contents of the robots.txt file hosted on www.example.com.

Another way is to use file_get_contents() function which will prove more handy if you want to read the remote file in just one PHP line:

$robotstxt = file_get_contents('http://www.example.com/robots.txt') or die('Unable to open remote file.');
Scroll to Top