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

📄 function.fread.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 read</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.fputs.html">fputs</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fscanf.html">fscanf</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.fread" class="refentry"> <div class="refnamediv">  <h1 class="refname">fread</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">fread</span> &mdash; <span class="dc-title">Binary-safe file read</span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">string</span> <span class="methodname"><b><b>fread</b></b></span>    ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$handle</tt></span>   , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$length</tt></span>   )</div>  <p class="para rdfs-comment">   <b>fread()</b> reads up to   <i><tt class="parameter">length</tt></i> bytes from the file pointer   referenced by <i><tt class="parameter">handle</tt></i>. Reading stops as soon as one   of the following conditions is met:   <ul class="itemizedlist">    <li class="listitem">     <span class="simpara">      <i><tt class="parameter">length</tt></i> bytes have been read     </span>    </li>    <li class="listitem">     <span class="simpara">      EOF (end of file) is reached     </span>    </li>    <li class="listitem">     <span class="simpara">      a packet becomes available (for network streams)     </span>    </li>    <li class="listitem">     <span class="simpara">      8192 bytes have been read (after opening userspace stream)     </span>    </li>   </ul>  </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">length</tt></i></span>     <dd>      <p class="para">       Up to <i><tt class="parameter">length</tt></i> number of bytes read.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns the read string or <b><tt>FALSE</tt></b> in case of error.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 A simple <b>fread()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;get&nbsp;contents&nbsp;of&nbsp;a&nbsp;file&nbsp;into&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br /></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">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Binary <b>fread()</b> example</b></p>    <div class="warning"><b class="warning">Warning</b>     <p class="para">      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.     </p>    </div>    <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">"c:\\files\\somepic.gif"</span><span style="color: #007700">;<br /></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">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #3 Remote <b>fread()</b> examples</b></p>    <div class="warning"><b class="warning">Warning</b>     <p class="para">      When reading from anything that is not a regular local file, such as      streams returned when      reading <a href="features.remote-files.html" class="link">remote files</a> or from      <a href="function.popen.html" class="function">popen()</a> and <a href="function.fsockopen.html" class="function">fsockopen()</a>, reading      will stop after a packet is available.  This means that you should      collect the data together in chunks as shown in the examples below.     </p>    </div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;For&nbsp;PHP&nbsp;5&nbsp;and&nbsp;up<br /></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: #DD0000">"http://www.example.com/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br />while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$contents&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8192</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div><div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>:        If you just want to get the contents of a file into a string, use    <a href="function.file-get-contents.html" class="function">file_get_contents()</a> as it has much better performance    than the code above.   <br />  </p></blockquote>  <blockquote><p><b class="note">Note</b>:        Note that <b>fread()</b> reads from the current position of     the file pointer. Use <a href="function.ftell.html" class="function">ftell()</a> to find the current     position of the pointer and <a href="function.rewind.html" class="function">rewind()</a> to rewind the     pointer position.   <br />  </p></blockquote> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</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.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a></li>    <li class="member"><a href="function.fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a></li>    <li class="member"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a></li>    <li class="member"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a></li>    <li class="member"><a href="function.fpassthru.html" class="function" rel="rdfs-seeAlso">fpassthru()</a></li>    <li class="member"><a href="function.ftell.html" class="function" rel="rdfs-seeAlso">ftell()</a></li>    <li class="member"><a href="function.rewind.html" class="function" rel="rdfs-seeAlso">rewind()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fputs.html">fputs</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fscanf.html">fscanf</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 + -