<!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>Truncates a file to a given length</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ftell.html">ftell</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fwrite.html">fwrite</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.ftruncate" class="refentry"> <div class="refnamediv"> <h1 class="refname">ftruncate</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ftruncate</span> — <span class="dc-title">Truncates a file to a given length</span></p> </div> <div class="refsect1 description" id="refsect1-function.ftruncate-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>ftruncate</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">$size</code></span> )</div> <p class="para rdfs-comment"> Takes the filepointer, <em><code class="parameter">handle</code></em>, and truncates the file to length, <em><code class="parameter">size</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.ftruncate-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"> The file pointer. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <em><code class="parameter">handle</code></em> must be open for writing. </p> </p></blockquote> </dd> </dt> <dt> <span class="term"><em><code class="parameter">size</code></em></span> <dd> <p class="para"> The size to truncate to. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If <em><code class="parameter">size</code></em> is larger than the file then the file is extended with null bytes. </p> <p class="para"> If <em><code class="parameter">size</code></em> is smaller than the file then the file is truncated to that size. </p> </p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.ftruncate-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.ftruncate-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.3.3</td> <td> Prior to this release <span class="function"><strong>ftruncate()</strong></span> returned an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> value of 1 on success, instead of <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> <strong><code>TRUE</code></strong>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.ftruncate-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2396"> <p><strong>Example #1 File truncation example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">'lorem_ipsum.txt'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</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">ftruncate</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </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">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </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">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.ftruncate-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The file pointer is <em class="emphasis">not</em> changed. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.ftruncate-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - Opens file or URL</span></li> <li class="member"> <span class="function"><a href="function.fseek.html" class="function" rel="rdfs-seeAlso">fseek()</a> - Seeks on 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.ftell.html">ftell</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fwrite.html">fwrite</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>