PHP Velho Oeste 2024

リモートファイルの使用

php.iniallow_url_fopenを有効にした場合、 ファイル名をパラメータとする関数の多くで HTTP および FTP のURL を使用することができます。加えて、 includeinclude_oncerequire および require_once 命令でURLを使用することができます (これらで使用するためには allow_url_include を有効にする必要があります)。 PHPがサポートしているプロトコルに関する詳細は サポートするプロトコル/ラッパーを参照ください。

例えば、リモートWebサーバーにファイルをオープンし、データを出力、デー タベースクエリーに使用するか、単にWebサイトのスタイルに合わせて出力 を行うことが可能です。

例1 リモートページのタイトルを得る

<?php
$file
= fopen ("http://www.example.com/", "r");
if (!
$file) {
echo
"<p>Unable to open remote file.\n";
exit;
}
while (!
feof ($file)) {
$line = fgets ($file, 1024);
/* タイトルとタグが同じ行にある場合のみ動作します。 */
if (preg_match ("@\<title\>(.*)\</title\>@i", $line, $out)) {
$title = $out[1];
break;
}
}
fclose($file);
?>

(正しいアクセス権限を有するユーザーとして接続した場合には) FTPサーバーにファイルを書き込むこともできます。 この方法では、新規ファイルを作成することのみができます。 既存のファイルを上書きしようとした場合には、 fopen()の処理は失敗します。

'anonymous' 以外のユーザーで接続を行う場合、URL の中で 'ftp://user:password@ftp.example.com/path/to/file' のようにユーザー名 (そして多分パスワードも) 指定する必要があります (Basic 認証を要求された際に HTTP 経由でファイルをアクセスする場合と同じ種類の構文を使用することができます)。

例2 リモートサーバーにデータを保存する

<?php
$file
= fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
if (!
$file) {
echo
"<p>Unable to open remote file for writing.\n";
exit;
}
/* データをここに書きます。 */
fputs ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file);
?>

注意:

上の例からリモートログに書きこむためにこの手法を使用することを考えるかも しれません。 しかし残念ながら、リモート上のファイルが既に存在する状態では fopen()をコールすることができないため、 それはできません。 分散ロギングのようなことを行うには、 syslog() の使用を考えてみてください。

add a note add a note

User Contributed Notes 4 notes

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

<?php
$ch
= curl_init("http://www.example.com/favicon.ico");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode >= 400 -> not found, $retcode = 200, found.
curl_close($ch);
?>
up
-4
anon
4 years 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.
up
-33
kalidass dot jst at gmail dot com
8 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);

  return($JsonResponse);
}
get_url("http://www.example.com");
up
-70
heck at fas dot harvard dot edu
19 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:
<?php
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:
<?php
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