PHP 7.4.8 Released!

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 include, include_once, require and require_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

fopen ("""r");
if (!
$file) {
"<p>Unable to open remote file.\n";
while (!
feof ($file)) {
$line fgets ($file1024);
/* This only works if the title and its tags are on one line */
if (preg_match ("@\<title\>(.*)\</title\>@i"$line$out)) {
$title $out[1];

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 fopen() call will fail.

To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as 'ftp://user:[email protected]/path/to/file'. (You can use the same sort of syntax to access files via HTTP when they require Basic authentication.)

Example #2 Storing data on a remote server

fopen ("""w");
if (!
$file) {
"<p>Unable to open remote file for writing.\n";
/* Write the data here. */
fwrite ($file$_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file);


You might get the idea from the example above that you can use this technique to write to a remote log file. Unfortunately that would not work because the fopen() call will fail if the remote file already exists. To do distributed logging like that, you should take a look at syslog().

add a note add a note

User Contributed Notes 4 notes

slva dot web dot sit at gmail dot com
6 years ago
If  allow_url_fopen is disabled in php.ini you can use CURL function for check file exist:

= curl_init("");

curl_setopt($ch, CURLOPT_NOBODY, true);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode >= 400 -> not found, $retcode = 200, found.
10 months ago
The following functions can't access external files if allow_url_fopen is disabled:

- fopen
- file_put_contents
- copy

I don't know if this is documented anywhere else, but I couldn't find it. Feel free to delete this post if so.
kalidass dot jst at gmail dot com
4 years ago
public function get_url($request_url) {

$curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $request_url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    $JsonResponse = curl_exec($curl_handle);
    $http_code = curl_getinfo($curl_handle);

heck at fas dot harvard dot edu
15 years ago
The previous post is part right, part wrong. It's part right because it's true that the php script will run on the remote server, if it's capable of interpreting php scripts. You can see this by creating this script on a remote machine:
echo system("hostname");
Then include that in a php file on your local machine. When you view it in a browser, you'll see the hostname of the remote machine.

However, that does not mean there are no security worries here. Just try replacing the previous script with this one:
echo "<?php system(\"hostname\"); ?>";
I'm guessing you can figure out what that's gonna do.

So yes, remote includes can be a major security problem.
To Top 飞禽走兽玩法 体彩浙江6+1 浙江6+1蓝球中了 河南省十一选五走势图带连线 浙江11选五走势图表下载 加拿大快乐8玩法介绍 宁夏新11选5开奖列表 下载浙江十一选五全天走势图 100元分辨真假钱窍门 福彩15选5带坐标走势图 免费pk10赛车计划软件官方 甘肃十一选五人工计划 重庆彩幸运农场走势图 百度 江西快3形态走势图 山东高频快乐扑克 体彩十一选五陕西 上海时时乐彩票怎么样才可以买