<!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>Seeks on a file pointer</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fscanf.html">fscanf</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fstat.html">fstat</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.fseek" class="refentry"> <div class="refnamediv"> <h1 class="refname">fseek</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">fseek</span> — <span class="dc-title">Seeks on a file pointer</span></p> </div> <div class="refsect1 description" id="refsect1-function.fseek-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>fseek</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$whence</code><span class="initializer"> = SEEK_SET</span></span> ] )</div> <p class="para rdfs-comment"> Sets the file position indicator for the file referenced by <em><code class="parameter">handle</code></em>. The new position, measured in bytes from the beginning of the file, is obtained by adding <em><code class="parameter">offset</code></em> to the position specified by <em><code class="parameter">whence</code></em>. </p> <p class="para"> In general, it is allowed to seek past the end-of-file; if data is then written, reads in any unwritten region between the end-of-file and the sought position will yield bytes with value 0. However, certain streams may not support this behavior, especially when they have an underlying fixed size storage. </p> </div> <div class="refsect1 parameters" id="refsect1-function.fseek-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">handle</code></em></span> <dd> <p class="para">A file system pointer <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> that is typically created using <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">offset</code></em></span> <dd> <p class="para"> The offset. </p> <p class="para"> To move to a position before the end-of-file, you need to pass a negative value in <em><code class="parameter">offset</code></em> and set <em><code class="parameter">whence</code></em> to <strong><code>SEEK_END</code></strong>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">whence</code></em></span> <dd> <p class="para"> <em><code class="parameter">whence</code></em> values are: <ul class="simplelist"> <li class="member"><strong><code>SEEK_SET</code></strong> - Set position equal to <em><code class="parameter">offset</code></em> bytes.</li> <li class="member"><strong><code>SEEK_CUR</code></strong> - Set position to current location plus <em><code class="parameter">offset</code></em>.</li> <li class="member"><strong><code>SEEK_END</code></strong> - Set position to end-of-file plus <em><code class="parameter">offset</code></em>.</li> </ul> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.fseek-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Upon success, returns 0; otherwise, returns -1. </p> </div> <div class="refsect1 examples" id="refsect1-function.fseek-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2393"> <p><strong>Example #1 <span class="function"><strong>fseek()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// read some data<br /></span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">4096</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// move back to the beginning of the file<br />// same as rewind($fp);<br /></span><span style="color: #0000BB">fseek</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.fseek-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If you have opened the file in append (<em>a</em> or <em>a+</em>) mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling <span class="function"><strong>fseek()</strong></span> will be undefined. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Not all streams support seeking. For those that do not support seeking, forward seeking from the current position is accomplished by reading and discarding data; other forms of seeking will fail. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.fseek-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.ftell.html" class="function" rel="rdfs-seeAlso">ftell()</a> - Returns the current position of the file read/write pointer</span></li> <li class="member"> <span class="function"><a href="function.rewind.html" class="function" rel="rdfs-seeAlso">rewind()</a> - Rewind the position of a file pointer</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="function.fscanf.html">fscanf</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fstat.html">fstat</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>