mirror of
https://github.com/php/doc-ja.git
synced 2026-03-26 16:12:17 +01:00
239 lines
6.9 KiB
XML
239 lines
6.9 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: bb6247b68128cab1b166b49a3a73a87f40333267 Maintainer: hirokawa Status: ready -->
|
|
<!-- CREDITS: shimooka -->
|
|
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.fwrite">
|
|
<refnamediv>
|
|
<refname>fwrite</refname>
|
|
<refpurpose>バイナリセーフなファイル書き込み処理</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type class="union"><type>int</type><type>false</type></type><methodname>fwrite</methodname>
|
|
<methodparam><type>resource</type><parameter>stream</parameter></methodparam>
|
|
<methodparam><type>string</type><parameter>data</parameter></methodparam>
|
|
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>length</parameter><initializer>&null;</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<simpara>
|
|
<function>fwrite</function>は<parameter>data</parameter>の内容を
|
|
<parameter>stream</parameter>が指しているファイル・ストリームに書き込みます。
|
|
</simpara>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>stream</parameter></term>
|
|
<listitem>
|
|
&fs.file.pointer;
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>data</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
書き込む文字列。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>length</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
<parameter>length</parameter>パラメータが数値の場合、
|
|
<parameter>length</parameter>バイト数分の書き込みが完了したか、
|
|
<parameter>data</parameter>が終わりに達したかのいずれか早い方の
|
|
事象により書き込みは中止されます。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<simpara>
|
|
<function>fwrite</function> は、
|
|
書き込んだバイト数を返します。
|
|
&return.falseforfailure;
|
|
</simpara>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
<para>
|
|
<function>fwrite</function> は、
|
|
失敗した場合に <constant>E_WARNING</constant> を発生させます。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
<parameter>length</parameter> は、nullable になりました。
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>簡単な <function>fwrite</function> の例</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$filename = 'test.txt';
|
|
$somecontent = "Add this to the file\n";
|
|
|
|
// ファイルが存在しかつ書き込み可能かどうか確認します
|
|
if (is_writable($filename)) {
|
|
|
|
// この例では$filenameを追加モードでオープンします。
|
|
// ファイルポインタはファイルの終端になりますので
|
|
// そこがfwrite()で$somecontentが追加される位置になります。
|
|
if (!$fp = fopen($filename, 'a')) {
|
|
echo "Cannot open file ($filename)";
|
|
exit;
|
|
}
|
|
|
|
// オープンしたファイルに$somecontentを書き込みます
|
|
if (fwrite($fp, $somecontent) === FALSE) {
|
|
echo "Cannot write to file ($filename)";
|
|
exit;
|
|
}
|
|
|
|
echo "Success, wrote ($somecontent) to file ($filename)";
|
|
|
|
fclose($fp);
|
|
|
|
} else {
|
|
echo "The file $filename is not writable";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
ネットワークストリームへの書き込みは、
|
|
すべての文字列を書き込み終える前に終了する可能性があります。
|
|
<function>fwrite</function> の戻り値を確かめるようにしましょう。
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function fwrite_stream($fp, $string) {
|
|
for ($written = 0; $written < strlen($string); $written += $fwrite) {
|
|
$fwrite = fwrite($fp, substr($string, $written));
|
|
if ($fwrite === false) {
|
|
return $written;
|
|
}
|
|
}
|
|
return $written;
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</para>
|
|
</note>
|
|
|
|
<note>
|
|
<para>
|
|
(Windowsのように)バイナリとテキストファイルの形式が異なるシステムにおいては、ファイルをオープンする際に
|
|
<function>fopen</function>の mode パラメータに 'b' を指定する必要があります。
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
<function>fopen</function> を使用して追記モードでオープンした
|
|
<parameter>stream</parameter> の場合、
|
|
<function>fwrite</function> はアトミックになります
|
|
(ただし、一部のプラットフォームにおいて <parameter>data</parameter>
|
|
がファイルシステムのブロックサイズを超えない場合、
|
|
そしてローカルファイルシステム上のファイルである場合に限ります)。
|
|
アトミックであるとは、つまり <function>fwrite</function>
|
|
をコールする前にリソースを <function>flock</function>
|
|
する必要がないということです。データの書き込みが中断されることはありません。
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
同じファイルポインタに 2 回書き込みを行うと、
|
|
データはファイルの末尾に追記されます。
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$fp = fopen('data.txt', 'w');
|
|
fwrite($fp, '1');
|
|
fwrite($fp, '23');
|
|
fclose($fp);
|
|
|
|
// 'data.txt' の中身は 123 となります。23 ではありません!
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>fread</function></member>
|
|
<member><function>fopen</function></member>
|
|
<member><function>fsockopen</function></member>
|
|
<member><function>popen</function></member>
|
|
<member><function>file_get_contents</function></member>
|
|
<member><function>pack</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
|
|
-->
|