PHP文件操作
<div style="display: none"> </div><div style="display: none"> </div>
<div style="display: none"> </div>
<h2>文件操作</h2>
<h3>(1)打开文件</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$file</span>=<span style="color: rgba(0, 128, 128, 1)">fopen</span>("test.txt", "w+");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作,以w+ 读与写的方式进行,文件不存在则自己创建</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 128, 128, 1)">fclose</span>(<span style="color: rgba(128, 0, 128, 1)">$file</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作完文档最好使用该函数去关闭文件</span>
<span style="color: rgba(0, 128, 128, 1)">5</span>
<span style="color: rgba(0, 128, 128, 1)">6</span> ?></span></pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190717210754805-1047436011.png" alt=""></p>
<p align="justify"> </p>
<blockquote>
<p align="justify"><span style="font-size: 14pt">fread(“打开的文件”,”读取文件大小”) //读取文件</span></p>
<p align="justify"><span style="font-size: 14pt">fgets(文件名)用于从文件中读取单行数据内容</span></p>
<p align="justify"><span style="font-size: 14pt">fgetc(文件名)用于从文件中读取单个字符</span></p>
<p align="justify"><span style="font-size: 14pt">feof()检测是否到达文件末尾</span></p>
</blockquote>
<p align="justify"> </p>
<blockquote>
<p align="justify"><strong><span style="font-size: 14pt; font-family: "Microsoft YaHei"">打开方式:</span></strong></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(153, 51, 0, 1)">r</span><strong><span style="color: rgba(153, 51, 0, 1)"> </span> </strong>只以<strong>读</strong>的方式去打开</span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(0, 0, 0, 1)">r+</span> <strong>读</strong>和<strong>写</strong></span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(51, 51, 0, 1)">w </span> <strong>只以写</strong>的方式打开,如果文件<strong>不存在</strong>,则<strong>创建</strong>文件</span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(0, 51, 0, 1)">w+</span> <strong>读</strong>和<strong>写</strong>,如果<strong>不存在</strong>,则<strong>创建</strong></span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(128, 128, 0, 1)">a </span> 文件<strong>末尾追加</strong>,如果文件<strong>不存在</strong>,则<strong>创建</strong>文件</span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(0, 0, 255, 1)">a+</span> <strong>读</strong>和<strong>追加</strong>,如文件<strong>不存在</strong>,则<strong>创建</strong>文件</span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(0, 204, 255, 1)">x </span> <strong>只写</strong> ,<strong>创建新文件</strong>,如文件<strong>已存在</strong>,返回<strong>false</strong>和错误</span></p>
<p align="justify"><span style="font-size: 14pt; font-family: "Microsoft YaHei""><span style="color: rgba(128, 0, 0, 1)">x+</span> <strong>读写</strong>,<strong>创建新文件</strong>,如文件<strong>已存在</strong>,返回<strong>false</strong>和错误</span></p>
</blockquote>
<p> </p>
<h3>(2)打开文件并进行读</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)"> 1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(128, 0, 128, 1)">$fp</span>=<span style="color: rgba(0, 128, 128, 1)">fopen</span>("test.txt", "r");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">只以读的方式打开test.txt</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(128, 0, 128, 1)">$fp</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 255, 1)">while</span> (!<span style="color: rgba(0, 128, 128, 1)">feof</span>(<span style="color: rgba(128, 0, 128, 1)">$fp</span><span style="color: rgba(0, 0, 0, 1)">)) {
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 128, 1)">$content</span>=<span style="color: rgba(0, 128, 128, 1)">fgets</span>(<span style="color: rgba(128, 0, 128, 1)">$fp</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">直到文档末尾,全读</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$content</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印读的内容</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 128, 128, 1)">fclose</span>(<span style="color: rgba(128, 0, 128, 1)">$fp</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭文件</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>
<span style="color: rgba(0, 128, 128, 1)">11</span> ?></span></pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190717212255435-35715075.png" alt="" style="display: block; margin-left: auto; margin-right: auto"></p>
<h3>(3)打开文件进行读取文件内容</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$file</span>=@<span style="color: rgba(0, 128, 128, 1)">fopen</span>("test.txt", "r"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(0, 128, 128, 1)">filesize</span>("test.txt")."<br>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(128, 0, 128, 1)">$file_contents1</span>=<span style="color: rgba(0, 128, 128, 1)">fread</span>(<span style="color: rgba(128, 0, 128, 1)">$file</span>, <span style="color: rgba(0, 128, 128, 1)">filesize</span>("test.txt"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$file_contents1</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 128, 128, 1)">fclose</span>(<span style="color: rgba(128, 0, 128, 1)">$file</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)">9</span> ?></span></pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190717213930416-1323563058.png" alt="" style="display: block; margin-left: auto; margin-right: auto"></p>
<h3 style="text-align: center">注:浏览器均没有以换行的方式显示</h3>
<h3>(4)写.txt文件</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$myfile</span> = <span style="color: rgba(0, 128, 128, 1)">fopen</span>("newfile.txt", "a+") or <span style="color: rgba(0, 0, 255, 1)">die</span>("Unable to open file!");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">w默认情况下会覆盖当前写的,a+(+多了可读,a 在追加)</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(128, 0, 128, 1)">$txt</span> = "Hello World!\n"<span style="color: rgba(0, 0, 0, 1)">;//要写入的内容
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 128, 1)">fwrite</span>(<span style="color: rgba(128, 0, 128, 1)">$myfile</span>, <span style="color: rgba(128, 0, 128, 1)">$txt</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 128, 128, 1)">fclose</span>(<span style="color: rgba(128, 0, 128, 1)">$myfile</span><span style="color: rgba(0, 0, 0, 1)">);//关闭文件
</span><span style="color: rgba(0, 128, 128, 1)">7</span>
<span style="color: rgba(0, 128, 128, 1)">8</span> ?></span></pre>
</div>
<p><span style="font-size: 14pt"> 创建一个newfile.txt 然后写入内容</span></p>
<p> <img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190728170956917-1607895214.jpg" alt="" style="display: block; margin-left: auto; margin-right: auto"></p>
<h3>(5)同上一个,将写入文件删除</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$file</span>="newfile.txt"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 128, 128, 1)">unlink</span>(<span style="color: rgba(128, 0, 128, 1)">$file</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除文件</span></span></pre>
</div>
<p> </p>
<p><span style="font-size: 14pt">再运行一遍:</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190728172013035-196577557.jpg" alt=""></p>
<h3 style="text-align: center">newfile.txt直接就被删除了</h3>
<h3> (6)读txt文件</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$filename</span>="./test.txt"<span style="color: rgba(0, 0, 0, 1)">;//读当前目录下的test.txt文档
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 128, 128, 1)">readfile</span>(<span style="color: rgba(128, 0, 128, 1)">$filename</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">5</span>
<span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span> ?></span></pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190729092522458-25927299.png" alt=""></p>
<p> </p>
<h3> (7)读图像文件</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$filename</span>="./1.jpg"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 128, 128, 1)">header</span>("content-type:image/png"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 128, 1)">readfile</span>(<span style="color: rgba(128, 0, 128, 1)">$filename</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span>
<span style="color: rgba(0, 128, 128, 1)">8</span> ?></span></pre>
</div>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190729094513392-1762188630.png" alt=""></p>
<h3> (8)以图像的形式显示文件</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$filename</span>="./test.txt"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(0, 128, 128, 1)">basename</span>(<span style="color: rgba(128, 0, 128, 1)">$filename</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 128, 128, 1)">header</span>("content-type:image/png"<span style="color: rgba(0, 0, 0, 1)">);//将文档以图像编码
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 128, 1)">readfile</span>(<span style="color: rgba(128, 0, 128, 1)">$filename</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span> ?></span></pre>
</div>
<p> </p>
<p><span style="font-size: 14pt">由于test.txt不是图片,因此不会显示其内容</span></p>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190729095359638-376977109.png" alt=""></p>
<h3> (8)下载文件</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14pt"><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$filenameurl</span>="test.txt"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 128, 128, 1)">header</span>("content-type:application/octet-stream"<span style="color: rgba(0, 0, 0, 1)">);//
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 128, 128, 1)">header</span>("content-Transfer-Encoding:utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 128, 1)">header</span>("content-dispostion:attachment;filename=\"".<span style="color: rgba(0, 128, 128, 1)">basename</span>(<span style="color: rgba(128, 0, 128, 1)">$filenameurl</span>)."\""<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 128, 128, 1)">readfile</span>(<span style="color: rgba(128, 0, 128, 1)">$filename</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">7</span>
<span style="color: rgba(0, 128, 128, 1)">8</span> ?></span></pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/1674381/201907/1674381-20190729100239169-1670193551.png" alt=""></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/cute-puli/p/11203886.html
頁:
[1]