-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathfread.xml
222 lines (214 loc) · 6.67 KB
/
fread.xml
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: ae5b5761e220b355014d0845e060ea1669befe7a Maintainer: hirokawa Status: ready -->
<!-- Credits: mumumu -->
<refentry xml:id="function.fread" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>fread</refname>
<refpurpose>バイナリセーフなファイルの読み込み</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>string</type><type>false</type></type><methodname>fread</methodname>
<methodparam><type>resource</type><parameter>stream</parameter></methodparam>
<methodparam><type>int</type><parameter>length</parameter></methodparam>
</methodsynopsis>
<para>
<function>fread</function> は、<parameter>stream</parameter>
が指すファイルポインタから最高 <parameter>length</parameter>
バイト読み込みます。
以下のいずれかの条件を満たしたら、読み込みを終了します。
<itemizedlist>
<listitem>
<simpara>
<parameter>length</parameter> バイトぶん読み込んだ
</simpara>
</listitem>
<listitem>
<simpara>
EOF (ファイルの終端) に達した
</simpara>
</listitem>
<listitem>
<simpara>
パケットが利用可能になるか、あるいは
<link linkend="function.socket-set-timeout">ソケットのタイムアウト</link>
が発生した (ネットワークストリームの場合)
</simpara>
</listitem>
<listitem>
<simpara>
バッファつきで読み込まれた、プレーンなファイルでないストリームの場合に、
一回の読み込みバイト数がチャンクサイズ (通常は 8192) に達した。
それまでにバッファされていたデータの内容によって、返されるデータのサイズはチャンクサイズより大きくなることがあります。
</simpara>
</listitem>
</itemizedlist>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>stream</parameter></term>
<listitem>
&fs.file.pointer;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>length</parameter></term>
<listitem>
<para>
最大 <parameter>length</parameter> バイトまで読み込む。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
読み込んだ文字列を返します。&return.falseforfailure;。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>シンプルな <function>fread</function> の例</title>
<programlisting role="php">
<![CDATA[
<?php
// ファイルの中身を読んで文字列に格納する
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>バイナリの <function>fread</function> の例</title>
<warning>
<para>
バイナリとテキストファイルの形式が異なるシステム(すなわち
Windows)では、<function>fopen</function>の mode パラメータに 'b'
を指定してファイルをオープンする必要があります。
</para>
</warning>
<programlisting role="php">
<![CDATA[
<?php
$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>リモートファイルの <function>fread</function> の例</title>
<warning>
<para>
通常のローカルファイル以外のもの、例えば
<link linkend="features.remote-files">リモートファイル</link> や
<function>popen</function>、<function>fsockopen</function> が返す
ストリームを読み込んでいる場合には、
パケットが有効になった後に読み込みはストップします。
つまり以下の例のように分割されたデータを結合すべきであるということです。
</para>
</warning>
<programlisting role="php">
<![CDATA[
<?php
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
]]>
</programlisting>
<programlisting role="php">
<![CDATA[
<?php
$handle = fopen("http://www.example.com/", "rb");
if (FALSE === $handle) {
exit("Failed to open stream to URL");
}
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
ファイルの中身を文字列に格納したいだけならば、<function>file_get_contents</function>
を使うほうが上記の例よりも効率的です。
</para>
</note>
<note>
<para>
<function>fread</function> は、
ファイルポインタが現在指している位置から読み込みを開始することに注意しましょう。
ポインタの現在位置を調べるには <function>ftell</function> を、
そしてポインタの位置を巻き戻すには <function>rewind</function> を使用します。
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>fwrite</function></member>
<member><function>fopen</function></member>
<member><function>fsockopen</function></member>
<member><function>popen</function></member>
<member><function>fgets</function></member>
<member><function>fgetss</function></member>
<member><function>fscanf</function></member>
<member><function>file</function></member>
<member><function>fpassthru</function></member>
<member><function>fseek</function></member>
<member><function>ftell</function></member>
<member><function>rewind</function></member>
<member><function>unpack</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->