📄 function.file-get-contents.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Reads entire file into a string</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.file-exists.html">file_exists</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.file-put-contents.html">file_put_contents</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.file-get-contents" class="refentry"> <div class="refnamediv"> <h1 class="refname">file_get_contents</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">file_get_contents</span> — <span class="dc-title">Reads entire file into a string</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>file_get_contents</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span> [, <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$context</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$maxlen</tt></span> ]]]] )</div> <p class="para rdfs-comment"> This function is similar to <a href="function.file.html" class="function">file()</a>, except that <b>file_get_contents()</b> returns the file in a <a href="language.types.string.html" class="type string">string</a>, starting at the specified <i><tt class="parameter">offset</tt></i> up to <i><tt class="parameter">maxlen</tt></i> bytes. On failure, <b>file_get_contents()</b> will return <b><tt>FALSE</tt></b>. </p> <p class="para"> <b>file_get_contents()</b> is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance. </p> <blockquote><p><b class="note">Note</b>: If you're opening a URI with special characters, such as spaces, you need to encode the URI with <a href="function.urlencode.html" class="function">urlencode()</a>. <br /> </p></blockquote> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">filename</tt></i></span> <dd> <p class="para"> Name of the file to read. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">flags</tt></i></span> <dd> <div class="warning"><b class="warning">Warning</b> <p class="para"> For all versions prior to PHP 6, this parameter is called <i><tt class="parameter">use_include_path</tt></i> and is a <a href="language.types.boolean.html" class="type bool">bool</a>. The <i><tt class="parameter">flags</tt></i> parameter is only available since PHP 6. If you use an older version and want to search for <i><tt class="parameter">filename</tt></i> in the <a href="ini.core.html#ini.include-path" class="link">include path</a>, this parameter must be <b><tt>TRUE</tt></b>. Since PHP 6, you have to use the <b><tt>FILE_USE_INCLUDE_PATH</tt></b> flag instead. </p> </div> <p class="para"> The value of <i><tt class="parameter">flags</tt></i> can be any combination of the following flags (with some restrictions), joined with the binary OR (<i>|</i>) operator. </p> <p class="para"> <table border="5"> <caption><b>Available flags</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Flag</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <b><tt>FILE_USE_INCLUDE_PATH</tt></b> </td> <td colspan="1" rowspan="1" align="left"> Search for <i><tt class="parameter">filename</tt></i> in the include directory. See <a href="ini.core.html#ini.include-path" class="link">include_path</a> for more information. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <b><tt>FILE_TEXT</tt></b> </td> <td colspan="1" rowspan="1" align="left"> If unicode semantics are enabled, the default encoding of the read data is UTF-8. You can specify a different encoding by creating a custom context or by changing the default using <b>stream_default_encoding()</b>. This flag cannot be used with <b><tt>FILE_BINARY</tt></b>. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <b><tt>FILE_BINARY</tt></b> </td> <td colspan="1" rowspan="1" align="left"> With this flag, the file is read in binary mode. This is the default setting and cannot be used with <b><tt>FILE_TEXT</tt></b>. </td> </tr> </tbody> </colgroup> </table> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">context</tt></i></span> <dd> <p class="para"> A valid context resource created with <a href="function.stream-context-create.html" class="function">stream_context_create()</a>. If you don't need to use a custom context, you can skip this parameter by <b><tt>NULL</tt></b>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">offset</tt></i></span> <dd> <p class="para"> The offset where the reading starts. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">maxlen</tt></i></span> <dd> <p class="para"> Maximum length of data read. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The function returns the read data or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.0</td> <td colspan="1" rowspan="1" align="left"> Added context support. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.1.0</td> <td colspan="1" rowspan="1" align="left"> Added the <i><tt class="parameter">offset</tt></i> and <i><tt class="parameter">maxlen</tt></i> parameters. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">6.0.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">use_include_path</tt></i> parameter was replaced by the <i><tt class="parameter">flags</tt></i> parameter. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: <span class="simpara">This function isbinary-safe.</span></p></blockquote> <div class="tip"><b class="tip">Tip</b><p class="simpara">A URL can be used as afilename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled.See <a href="function.fopen.html" class="function">fopen()</a> for more details on how to specifythe filename and <a href="wrappers.html" class="xref">List of Supported Protocols/Wrappers</a> for a list of supportedURL protocols.</p></div> <div class="warning"><b class="warning">Warning</b><p class="para">When using SSL, Microsoft IISwill violate the protocol by closing the connection without sending a<i>close_notify</i> indicator. PHP will report this as "SSL: FatalProtocol Error" when you reach the end of the data. To work around this, thevalue of <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> should belowered to a level that does not include warnings.PHP 4.3.7 and higher can detect buggy IIS server software when you openthe stream using the <i>https://</i> wrapper and will suppress thewarning. When using <a href="function.fsockopen.html" class="function">fsockopen()</a> to create an<i>ssl://</i> socket, the developer is responsible for detectingand suppressing this warning.</p></div> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a></li> <li class="member"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a></li> <li class="member"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a></li> <li class="member"><a href="function.readfile.html" class="function" rel="rdfs-seeAlso">readfile()</a></li> <li class="member"><a href="function.file-put-contents.html" class="function" rel="rdfs-seeAlso">file_put_contents()</a></li> <li class="member"><a href="function.stream-get-contents.html" class="function" rel="rdfs-seeAlso">stream_get_contents()</a></li> <li class="member"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.file-exists.html">file_exists</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.file-put-contents.html">file_put_contents</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 + -