<!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>Outputs a file</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.popen.html">popen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.readlink.html">readlink</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.readfile" class="refentry"> <div class="refnamediv"> <h1 class="refname">readfile</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">readfile</span> — <span class="dc-title">Outputs a file</span></p> </div> <div class="refsect1 description" id="refsect1-function.readfile-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>readfile</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ]] )</div> <p class="para rdfs-comment"> Reads a file and writes it to the output buffer. </p> </div> <div class="refsect1 parameters" id="refsect1-function.readfile-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">filename</code></em></span> <dd> <p class="para"> The filename being read. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">use_include_path</code></em></span> <dd> <p class="para"> You can use the optional second parameter and set it to <strong><code>TRUE</code></strong>, if you want to search for the file in the <a href="ini.core.html#ini.include-path" class="link">include_path</a>, too. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <p class="para"> A context stream <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.readfile-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the number of bytes read from the file. If an error occurs, <strong><code>FALSE</code></strong> is returned and unless the function was called as @ <span class="function"><strong>readfile()</strong></span>, an error message is printed. </p> </div> <div class="refsect1 examples" id="refsect1-function.readfile-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2422"> <p><strong>Example #1 Forcing a download using <span class="function"><strong>readfile()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$file </span><span style="color: #007700">= </span><span style="color: #DD0000">'monkey.gif'</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Description: File Transfer'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type: application/octet-stream'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Disposition: attachment; filename='</span><span style="color: #007700">.</span><span style="color: #0000BB">basename</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">));<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Transfer-Encoding: binary'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Expires: 0'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Cache-Control: must-revalidate'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pragma: public'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Length: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">));<br /> </span><span style="color: #0000BB">ob_clean</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">flush</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /> exit;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="mediaobject"> <div class="imageobject"> <img src="images/e88cefb5c3fca5060e2490b9763c4433-readfile.png" alt="Open / Save dialogue" width="319" height="245" /> </div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.readfile-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <span class="function"><strong>readfile()</strong></span> will not present any memory issues, even when sending large files, on its own. If you encounter an out of memory error ensure that output buffering is off with <span class="function"><a href="function.ob-get-level.html" class="function">ob_get_level()</a></span>. </p> </p></blockquote> <div class="tip"><strong class="tip">Tip</strong><p class="simpara">A URL can be used as a filename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled. See <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for more details on how to specify the filename. See the <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.</p></div> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Context support was added with PHP 5.0.0. For a description of <em>contexts</em>, refer to <a href="book.stream.html" class="xref">Streams</a>.</span></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.readfile-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.fpassthru.html" class="function" rel="rdfs-seeAlso">fpassthru()</a> - Output all remaining data on a file pointer</span></li> <li class="member"> <span class="function"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a> - Reads entire file into an array</span></li> <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.include.html" class="function" rel="rdfs-seeAlso">include</a> - include</span></li> <li class="member"> <span class="function"><a href="function.require.html" class="function" rel="rdfs-seeAlso">require</a> - require</span></li> <li class="member"> <span class="function"><a href="function.virtual.html" class="function" rel="rdfs-seeAlso">virtual()</a> - Perform an Apache sub-request</span></li> <li class="member"> <span class="function"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Reads entire file into a string</span></li> <li class="member"><a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a></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.popen.html">popen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.readlink.html">readlink</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>