⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.fwrite.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Binary-safe file write</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ftruncate.html">ftruncate</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.glob.html">glob</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.fwrite" class="refentry"> <div class="refnamediv">  <h1 class="refname">fwrite</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">fwrite</span> &mdash; <span class="dc-title">Binary-safe file write</span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">int</span> <span class="methodname"><b><b>fwrite</b></b></span>    ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$handle</tt></span>   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$string</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$length</tt></span>  ] )</div>  <p class="simpara">   <b>fwrite()</b> writes the contents of   <i><tt class="parameter">string</tt></i> to the file stream pointed to by   <i><tt class="parameter">handle</tt></i>.   </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">handle</tt></i></span>     <dd>      <p class="para">A file system pointer <a href="language.types.resource.html" class="type resource">resource</a>that is typically created using <a href="function.fopen.html" class="function">fopen()</a>.</p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">string</tt></i></span>     <dd>      <p class="para">       The string that is to be written.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">length</tt></i></span>     <dd>      <p class="para">       If the <i><tt class="parameter">length</tt></i> argument is given, writing will       stop after <i><tt class="parameter">length</tt></i> bytes have been written or       the end of <i><tt class="parameter">string</tt></i> is reached, whichever comes       first.      </p>      <p class="para">       Note that if the <i><tt class="parameter">length</tt></i> argument is given,       then the <a href="info.configuration.html#ini.magic-quotes-runtime" class="link">magic_quotes_runtime</a>       configuration option will be ignored and no slashes will be       stripped from <i><tt class="parameter">string</tt></i>.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="simpara">   <b>fwrite()</b> returns the number of bytes   written, or <b><tt>FALSE</tt></b> on error.  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>:        On systems which differentiate between binary and text files    (i.e. Windows) the file must be opened with &#039;b&#039; included in    <a href="function.fopen.html" class="function">fopen()</a> mode parameter.   <br />  </p></blockquote>  <blockquote><p><b class="note">Note</b>:        If <i><tt class="parameter">handle</tt></i> was <a href="function.fopen.html" class="function">fopen()</a>ed in    append mode, <b>fwrite()</b>s are atomic (unless the size of    <i><tt class="parameter">string</tt></i> exceeds the filesystem&#039;s block size, on some    platforms, and as long as the file is on a local filesystem). That is,    there is no need to <a href="function.flock.html" class="function">flock()</a> a resource before calling    <b>fwrite()</b>; all of the data will be written without    interruption.   <br />  </p></blockquote>  <blockquote><p><b class="note">Note</b>:        If writing twice to the file pointer, then the data will be appended to     the end of the file content, meaning that the example below wouldn&#039;t work     as expected:        <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'data.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'23'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;content&nbsp;of&nbsp;'data.txt'&nbsp;is&nbsp;now&nbsp;123&nbsp;and&nbsp;not&nbsp;23!<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>        </div>   <br />  </p></blockquote> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 A simple <b>fwrite()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test.txt'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$somecontent&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Add&nbsp;this&nbsp;to&nbsp;the&nbsp;file\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Let's&nbsp;make&nbsp;sure&nbsp;the&nbsp;file&nbsp;exists&nbsp;and&nbsp;is&nbsp;writable&nbsp;first.<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_writable</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">))&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;In&nbsp;our&nbsp;example&nbsp;we're&nbsp;opening&nbsp;$filename&nbsp;in&nbsp;append&nbsp;mode.<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;The&nbsp;file&nbsp;pointer&nbsp;is&nbsp;at&nbsp;the&nbsp;bottom&nbsp;of&nbsp;the&nbsp;file&nbsp;hence<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;that's&nbsp;where&nbsp;$somecontent&nbsp;will&nbsp;go&nbsp;when&nbsp;we&nbsp;fwrite()&nbsp;it.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Cannot&nbsp;open&nbsp;file&nbsp;($filename)"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Write&nbsp;$somecontent&nbsp;to&nbsp;our&nbsp;opened&nbsp;file.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$somecontent</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Cannot&nbsp;write&nbsp;to&nbsp;file&nbsp;($filename)"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Success,&nbsp;wrote&nbsp;($somecontent)&nbsp;to&nbsp;file&nbsp;($filename)"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /><br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;file&nbsp;$filename&nbsp;is&nbsp;not&nbsp;writable"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a></li>    <li class="member"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a></li>    <li class="member"><a href="function.fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a></li>    <li class="member"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a></li>    <li class="member"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ftruncate.html">ftruncate</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.glob.html">glob</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -