<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Read from file</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="splfileobject.fputcsv.html">SplFileObject::fputcsv</a></div> <div class="next" style="text-align: right; float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf</a></div> <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="splfileobject.fread" class="refentry"> <div class="refnamediv"> <h1 class="refname">SplFileObject::fread</h1> <p class="verinfo">(PHP 5 >= 5.5.11, PHP 7)</p><p class="refpurpose"><span class="refname">SplFileObject::fread</span> — <span class="dc-title">Read from file</span></p> </div> <div class="refsect1 description" id="refsect1-splfileobject.fread-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><strong>SplFileObject::fread</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> )</div> <p class="para rdfs-comment"> Reads the given number of bytes from the file. </p> </div> <div class="refsect1 parameters" id="refsect1-splfileobject.fread-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">length</code></dt> <dd> <p class="para"> The number of bytes to read. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-splfileobject.fread-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the string read from the file or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-splfileobject.fread-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4887"> <p><strong>Example #1 <span class="methodname"><strong>SplFileObject::fread()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Get contents of a file into a string<br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= new </span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">$file</span><span style="color: #007700">-></span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">-></span><span style="color: #0000BB">getSize</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-splfileobject.fread-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Note that <span class="methodname"><strong>SplFileObject::fread()</strong></span> reads from the current position of the file pointer. Use <span class="methodname"><a href="splfileobject.ftell.html" class="methodname">SplFileObject::ftell()</a></span> to find the current position of the pointer and <span class="methodname"><a href="splfileobject.rewind.html" class="methodname">SplFileObject::rewind()</a></span> (or <span class="methodname"><a href="splfileobject.fseek.html" class="methodname">SplFileObject::fseek()</a></span>) to rewind the pointer position. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-splfileobject.fread-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="splfileobject.fputcsv.html">SplFileObject::fputcsv</a></div> <div class="next" style="text-align: right; float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf</a></div> <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>