mirror of
https://github.com/php/doc-zh.git
synced 2026-03-23 22:52:08 +01:00
* Fix features/: translations, security, missing content - remote-files.xml: remove duplicated 如果 - sessions.xml: 新引力→吸引力 - persistent-connections.xml: fix 自以为→用作 translation error - gc.xml: remove translator comment left in published text - http-auth.xml: add htmlspecialchars() for XSS protection, sync with EN - file-upload.xml: translate 2 untranslated English paragraphs, add missing "Uploading an entire directory" section * Update file-upload.xml 根据 https://github.com/php/doc-zh?tab=readme-ov-file#有关在翻译后的中文文件中的空格与换行的说明 更新内容换行 --------- Co-authored-by: mowangjuanzi <mowangjuanzi@petalmail.com>
103 lines
3.3 KiB
XML
103 lines
3.3 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: a0ae28d3bc85f927c22649ebd9a590b921534b7d Maintainer: thomaslio Status: ready -->
|
||
<!-- CREDITS: Gregory, mowangjuanzi -->
|
||
<chapter xml:id="features.remote-files" xmlns="http://docbook.org/ns/docbook">
|
||
<title>使用远程文件</title>
|
||
|
||
<para>
|
||
只要在 &php.ini; 中启用 <option>allow_url_fopen</option>,就可以将 <acronym>HTTP</acronym>
|
||
和 <acronym>FTP</acronym> URL 与大多数以文件名作为参数的函数一起使用。此外,也可以在
|
||
<function>include</function>、<function>include_once</function>、<function>require</function>
|
||
及 <function>require_once</function> 语句中使用 URL(必须启用
|
||
<option>allow_url_include</option>)。PHP 协议支持的更多信息参见<xref linkend="wrappers"/>。
|
||
</para>
|
||
<para>
|
||
例如,可以用此打开远程 web
|
||
服务器上的文件,解析输出以获取所需数据,然后在数据库查询中使用该数据,或者网站其余部分相同的样式输出内容。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>获取远程页面的标题</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?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);
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
也可以在 FTP
|
||
服务器上写入文件(提供具有正确访问权限的用户身份连接)。只能使用此方法创建新文件;如果尝试覆盖已存在的文件,则调用
|
||
<function>fopen</function> 将失败。
|
||
</para>
|
||
<para>
|
||
要以“anonymous”以外的用户名连接服务器,需要指明用户名(可能还有密码),例如“<literal>ftp://user:password@ftp.example.com/path/to/file</literal>”(当需要基础认证的
|
||
HTTP 协议访问远程文件时也可以使用相同的语法)。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>将数据保存到远程服务器</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
|
||
if (!$file) {
|
||
echo "<p>Unable to open remote file for writing.\n";
|
||
exit;
|
||
}
|
||
/* 这里写入数据。 */
|
||
fwrite ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");
|
||
fclose ($file);
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<note>
|
||
<para>
|
||
或许可以从以上范例中了解到可以使用该技术写入远程日志文件。但不幸的是,这不起作用,因为如果远程文件已存在,调用
|
||
<function>fopen</function> 将失败。要进行这样的分布式日志记录,应该参考 <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:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|