1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
<?xml version="1.0" encoding="utf-8"?>
<chapter id="features.remote-files">
<title>リモートファイルの使用</title>
<para>
PHPを設定する際に "URL fopen wrapper" に関するサポートを
有効にした場合(つまり、<option>--disable-url-fopen-wrapper</option>
フラグを configure に明示的に渡した場合(4.0.3までのバージョン)、ま
たは、php.ini で <parameter>allow_url_fopen</parameter> を off に設
定した場合(より新しいバージョン)を除いて)、
<function>require</function> および <function>include</function> 文を
含むファイル名をパラメータとして受ける多くの関数
においてHTTPおよびFTP URLを使用することができます。
</para>
<para>
<note>
<para>
Windows上では、リモートファイルに <function>include</function> および
<function>require</function> 文を使用することはできません。
</para>
</note>
</para>
<para>
例えば、リモートWebサーバーにファイルをオープンし、データを出力、デー
タベースクエリーに使用するか、単にWebサイトのスタイルに合わせて出力
を行うことが可能です。
</para>
<para>
<example>
<title>リモートページのタイトルを得ます</title>
<programlisting role="php">
<![CDATA[
<?php
$file = fopen("http://www.php.net/", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof($file)) {
$line = fgets($file, 1024);
/* タイトルとタグが同じ行にある場合のみ動作します。 */
if (eregi("<title>(.*)</title>", $line, $out)) {
$title = $out[1];
break;
}
}
fclose($file);
?>
]]>
</programlisting>
</example>
</para>
<para>
適当なアクセス権を持ったユーザーで接続を行い、書きこもうとするファ
イルがまだ存在しない場合、FTPでファイルを書きこむことも可能です。
'anonymous'以外のユーザーで接続を行う場合、URLの中で
'ftp://user:password@ftp.example.com/path/to/file' のように
ユーザー名(そして多分パスワードも)指定する必要があります。
(Basic認証を要求された際にHTTP経由でファイルをアクセスする場合と
同じ種類の構文を使用することができます。)
</para>
<para>
<example>
<title>リモートサーバーにデータを保存する</title>
<programlisting role="php">
<![CDATA[
<?php
$file = fopen("ftp://ftp.php.net/incoming/outputfile", "w");
if (!$file) {
echo "<p>Unable to open remote file for writing.\n";
exit;
}
/* データをここに書きます。 */
fputs($file, "$HTTP_USER_AGENT\n");
fclose($file);
?>
]]>
</programlisting>
</example>
</para>
<para>
<note>
<para>
上の例からリモートログに書きこむためにこの手法を使用することを考えるかも
しれません。
しかし、上に述べたように、URL fopen() ラッパーを使って書きこめるのは
新規のファイルのみです。
ログのようなことを行うには、
<function>syslog</function> の使用を考えてみて下さい。
</para>
</note>
</para>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
|