PHP函数介绍—fopen(): 打开文件或URL
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PHP中,<span style="color: black;">运用</span>fopen()函数<span style="color: black;">能够</span>打开一个文件<span style="color: black;">或</span>URL。fopen()函数是一个非常常用的函数,<span style="color: black;">能够</span>用于读取<span style="color: black;">或</span>写入数据到文件<span style="color: black;">或</span>URL。</p><span style="color: black;">语法:</span>resource fopen ( string <span style="color: black;">$filename</span> , string <span style="color: black;">$mode</span> [, bool <span style="color: black;">$use_include_path</span> = <span style="color: black;">false</span> [, resource <span style="color: black;">$context</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ]] )</p><span style="color: black;">参数:</span>filename:必需。指定要打开的文件<span style="color: black;">或</span>URL。mode:必需。指定打开文件的模式。常用的模式有 "r"(只读)、"w"(只写)、"a"(追加写入)、"x"(新建),以及 "b"(二进制模式)等。use_include_path:可选。布尔值,指定<span style="color: black;">是不是</span><span style="color: black;">运用</span> include_path 来搜索文件。context:可选。用于设置流的<span style="color: black;">各样</span>参数。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面是<span style="color: black;">有些</span><span style="color: black;">运用</span>fopen()函数的示例:</p><span style="color: black;">示例1:读取文件内容</span><span style="color: black;">$filename</span> = <span style="color: black;">"example.txt"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$file</span> = fopen(<span style="color: black;">$filename</span>, <span style="color: black;">"r"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">if</span> (<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p><span style="color: black;">while</span>(!feof(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)) {</p> <span style="color: black;">echo</span> fgets(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>fclose(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>} <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p><span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">没法</span>打开文件!"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?></p><span style="color: black;">示例2:写入文件内容</span><span style="color: black;">$filename</span> = <span style="color: black;">"example.txt"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$file</span> = fopen(<span style="color: black;">$filename</span>, <span style="color: black;">"w"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">if</span> (<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p>fwrite(<span style="color: black;">$file</span>, <span style="color: black;">"Hello, World!"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>fclose(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>} <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p><span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">没法</span>打开文件!"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?></p><span style="color: black;">示例3:追加写入文件内容</span><span style="color: black;">$filename</span> = <span style="color: black;">"example.txt"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$file</span> = fopen(<span style="color: black;">$filename</span>, <span style="color: black;">"a"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">if</span> (<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p>fwrite(<span style="color: black;">$file</span>, <span style="color: black;">"Hello, PHP!"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>fclose(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>} <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p><span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">没法</span>打开文件!"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?></p><span style="color: black;">示例4:打开URL</span><span style="color: black;">$url</span> = <span style="color: black;">"http://www.example.com"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$file</span> = fopen(<span style="color: black;">$url</span>, <span style="color: black;">"r"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">if</span> (<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p><span style="color: black;">while</span> (!feof(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)) {</p> <span style="color: black;">echo</span>fgets(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>fclose(<span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>} <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p><span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">没法</span>打开URL!"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">总结:</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fopen()函数是一个非常实用的PHP函数,<span style="color: black;">能够</span>用来打开文件<span style="color: black;">或</span>URL进行数据读取和写入。在<span style="color: black;">运用</span>该函数时,需要<span style="color: black;">重视</span>文件<span style="color: black;">或</span>URL的权限以及指定的模式<span style="color: black;">是不是</span>正确。<span style="color: black;">同期</span>,<span style="color: black;">必定</span>要记得在操作完成后关闭打开的文件句柄,以释放资源。</p>
页:
[1]