Home  >  Article  >  Backend Development  >  PHP file writing and reading graphics and text details

PHP file writing and reading graphics and text details

墨辰丷
墨辰丷Original
2018-05-22 14:16:251402browse

This article mainly introduces the details of writing and reading graphics and text in PHP files. Interested friends can refer to it. I hope it will be helpful to everyone. Article outline:

1. The basic idea of ​​​​implementing file reading and writing

2. Open the file using the fopen method

3. File reading and file writing operations

#4. Use the fclose method to close the file

#5. Movement of the file pointer

#6. Carriage return and line feed under Windows and UNIX

1. Basic ideas for realizing file reading and writing:

1. Open the file through the fopen method: $fp =fopen(/*parameter, parameter*/), fp is Resource type
2. Perform file reading or file writing operations (the function used here takes the $fp returned in 1 as a parameter)
3. Call fclose($fp) to close the file

Two: Open the file using the fopen method

##fopen(file path[string], open mode[string])

f35d6e602fd7d0f0edfa6f7d103c1b57The first of fopen The first parameter is the file path


The way to write the file path: 1 absolute path, 2 relative path

##1 absolute path: Friends who work under Windows should be familiar with it. The path separator under Windows is "\" instead of "/", but we cannot use the official "\" as the separator when writing the path.

#What happens if we write the path with the "\" delimiter?

<?php
   $fp = fopen("C:\wamp64\www\text.txt",&#39;w&#39;);
?>


An error message is reported after running, indicating that the path parameter is invalid


So we need to replace the separator "\" with "/":

<?php
  $fp = fopen("C:/wamp64/www/text.txt",&#39;w&#39;);
?>


None at runtime An error is reported, indicating that the parameters are valid.

[Note]The fopen function cannot understand the "\" delimiter. If you want to use "\", you must use escape, such as It is also possible to write: "C:\\wamp64\\www\\text.txt", and the function can understand it without reporting an error. But even so, it is not recommended to use "\", because under OS (mac), only "/" can be recognized but not "\"

This section Conclusion: It is recommended to stick to using "/" as the delimiter

2. Relative path: The previous section introduced how to write absolute paths, but this brings another problem: the directory structure of the server may change significantly, and then all the original absolute paths will be rewritten. For example, in my The target file path on my computer is C:/wamp64/www/text.txt. What if I rename the www folder to penghuwan? The path parameters originally written are invalid. So we introduced the relative path writing method:

<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;w&#39;);
?>

##• $_SERVER is a super global variable of PHP (can be used anywhere in the code Access, type is array), the default root directory of the server can be obtained through $_SERVER['DOCUMENT_ROOT']

The default root directory of the server can be modified through php.ini (this can be Baidu yourself)

• $_SERVER['DOCUMENT_ROOT'] here is equivalent to C:/wamp64/www


##Conclusion of this section: It is recommended to use relative paths

2cc198a1d5eb0d3eb508d858c9f5cbdbThe second parameter of fopen is the open mode
After setting the open mode, we are equivalent to setting permissions for the subsequent read and write operations:

The most basic modes:

"r": Files can only be read, but files cannot be written (write operations are ignored)"w": Can only write files, cannot read files (read operations are ignored) "a": Only append files, similar to "w", the difference is that "w" deletes the original content , "a" does not delete the original content, but only appends the content



<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;w&#39;);
  fwrite($fp,&#39;在写模式下写入&#39;);
  fclose($fp);
?>


After setting the permission for the write operation, it can work normally The file is written to the file


After running, open C:/wamp64/www/text.txt:

This time we set the permission to read-only. and try to write the text: 'Write in read-only mode'

<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
  fwrite($fp,&#39;在读模式下写入&#39;);
  fclose($fp);
?>


Open C:/wamp64/www/ after running text.txt, it was found that the file content has not changed, indicating that the operation was ignored because the corresponding permissions were not set


关于打开模式的网络资料,我想大家最可能找到的是这张表:(图来自W3C)

很全面,但我觉得这张表对新手有些不太友好,让人看后不知多云。 r是只读,w是只写(原来有的内容全删除),a是追加(不删除原有内容),这都好理解。

但r+,w+,和a+的区别和联系讲的实在太模糊了呀。 这里我就想详细地讲一下r+,w+,和a+三者的区别和联系:

首先r+,w+,和a+都是可读可写的,读取时的方式是一样的,关键在于写入方式的不同:

r+: 从文件[头部][覆盖]原有内容 ([不删除]原有内容);

a+:从文件[尾部][追加]内容 ([不删除]原有内容);

w+:[完全删除]原有内容,然后[再添加]新的内容

下面我依次演示上述的结论,首先我们没有写入的时候文本是”I am initialized value”(意为我是初始值)

采用r+模式写入文本“r+ mode”


<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r+&#39;);
  fwrite($fp,&#39;r+ mode&#39;);
  fclose($fp);
?>


运行后再打开文本,发现“I am in”被“r+ mode”覆盖了:

采用a+模式写入文本“a+ mode”

基于”I am initialized value”的初始文本我们运行以下代码:


<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;a+&#39;);
  fwrite($fp,&#39;a+ mode&#39;);
  fclose($fp);
?>



I am initialized value没有被删除和覆盖,而是在后面追加了a+ mode的这一段新文本

运行多次后:

•采用w+模式写入文本“w+ mode”

基于”I am initialized value”的初始文本我们运行以下代码:


<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;w+&#39;);
  fwrite($fp,&#39;w+ mode&#39;);
  fclose($fp);
?>


运行后,我们发现”I am initialized value”已经被删除了,然后才加上了“w+ mode”这段新文本

【注意】r+,a+,w+还有一个区别是a+,w+在文件不存在时则创建文件,r+文件不存在时报错

【吐槽】:关于r+和w+,a+的区别,我找了网络上,包括W3C和各种博客文章以及那本“PHP圣经”上的各种资料,发现都是一笔带过去的,这也是我写这篇文章的原因

三.文件读取和文件写入操作

先说说几个比较重要的函数:

file_exists():判断文件是否存在,返回布尔值

filesize():判断一个文件大小,返回文件的字节数,为整型数字

unlink():删除一个文件

写入文件

fwrite(资源文件对象[string],写入方式[string]),资源文件对象即为fopen方法返回的参数,为Resource类型,写入方式可以是w(或者w+,a+,r+)

已经有上面的例子,这里就不放demo了

读取文件

这是我们要读取的文件内容:


读取文件的方式有以下几种:

1.一次读取一个字节的数据 fgetc()

2.一次读取指定的字节数的数据 fread()

3.一次读取一行数据 fgets()/fgetcsv()

4.一次读完全部数据 fpassthru()/ file()

1. 一次读取一个字节 —— 通过fgetc()获取单个字节


<?php
   $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
   $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);//打开文件
   if(file_exists("$DOCUMENT_ROOT/text.txt")){//当文件存在时,才读取内容
     while(!feof($fp)){//判断文件指针是否到达末尾
        $c = fgetc($fp);//每执行一次fgetc(),文件指针就向后移动一位
        echo $c;//输出获取到的字节
      }
    }
   fclose($fp);//关闭文件
?>


运行:

【注意】:无论是按文本格式输入输出还是按二进制格式输出,fgetc()每次获取的是一个字节而不是一个字符

上面的例子中我们是逐个输出,现在让我们只做一次输出,看看结果怎样:


<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
  echo fgetc($fp);//只做一次输出
  close($fp);
?>


运行结果如下,我们得到的不是汉字“我”,而是一个乱码,其实这个乱码就是一个字节


<?php
   $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
   $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
   echo fgetc($fp);//连续做三次输出
   echo fgetc($fp);
   echo fgetc($fp);
   fclose($fp);
?>


2.一次读取多个字节 ——通过fread()方法:


<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
  echo fread($fp, 3);//一次输出三个字节即一个汉字字符(UTF-8)
  fclose($fp);
?>


运行结果:


改成:


echo fread($fp, 6);


运行结果如下,输出了6个字节也即两个汉字字符(UTF-8)

3.一次读取一行——通过fgets()获取一行内容


<?php
    $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;]
    $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);//打开文件
    if(file_exists("$DOCUMENT_ROOT/text.txt")){//当文件存在时,才读取内容
     while(!feof($fp)){//判断文件指针是否到达末尾
       $line = fgets($fp);//返回一行文本,并将文件指针移动到下一行头部
       echo $line."<br/>";//输出获取到的一行文本
     }
    }
    fclose($fp);//关闭文件
?>


fgets()其实还有第二个参数,这个参数规定了每一行能读取的最大字节数(注意是字节数不是字符数):

【注意】在UTF-8编码下汉字3字节,字母1字节

下面我修改上面的一行,代码,使获取的每一行最大字符数为3(也即字节数为9)


$line = fgets($fp,10);


Demo:

【注意】:这里我fgets()里第二个参数为10,为什么是10呢?因为

1.这里的长度是按字节数算的

2.一个汉字占3个字节。fgets($fp,10)代表一次最多读取10 - 1 = 9字节

4.一次读完全部文件 ——fpassthru() or file()?

fpassthru()将读取文件并直接输出(无处理过程)


<?php
   $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
   $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
   fpassthru($fp);
   fclose($fp);
?>


运行结果:

【注意】这里需要注意一点的是,我们并没有从fpassthru($fp)获取到返回值然后echo到页面上去,也就是说这个方法是会强制输出获取的内容的,而并不是像之前例子的方法那样返回文本,允许我们保存到变量中才将其输出

将读取到的全部内容保存到一个数组中,每个数组元素为一行的内容——fille()


<?php
  $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
  $file_array = file("$DOCUMENT_ROOT/text.txt");//取到文件数组
  foreach ($file_array as $value) {//输出数组元素
    echo $value."<br/>";
  }
?>


【注意】:这里我们并不需要写fopen和fclose哦!也就是说file()方法已经帮我们做了这一步了

四.使用fclose方法关闭文件

fclose()将返回一个布尔值,成功关闭为true,关闭失败为false(失败的情况很少出现,可不考虑)

是否打开文件后一定要关闭?

1即使不手写fclose,在PHP脚本执行结束后,也会自动关闭文件的

2但在一个长时间执行的脚本中,如果不写关闭文件的fclose(),在文件加锁的情况下会造成操作的阻塞,所以,写fclose是个好习惯

五.文件指针的移动

我们上面调用的读取文件的函数,其实都是基于文件指针去打印的,每读取一段字节内容,文件指针就向后移动一段字节长度,直到被读取的文件最大字节长度为止


<?php
     $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
     function print_file_pointer($fp){//定义一个打印文件指针位置的函数
       echo " <br/>//此时文件指针的位置:";
       echo ftell($fp)."<br/>";
     }
     $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
     echo fgetc($fp);//通过fgetc连续输出三个字节
     echo fgetc($fp);
     echo fgetc($fp);
     print_file_pointer($fp);//打印此刻文件指针的位置
     
     echo fread($fp,6);//通过fread一次输出6字节
     print_file_pointer($fp);//打印此刻文件指针的位置
     
     echo fgets($fp); //通过fgets输出一整行
     print_file_pointer($fp);//打印此刻文件指针的位置
     
     fpassthru($fp); //一次性输出全部内容
     print_file_pointer($fp);//打印此刻文件指针的位置
     
     fseek($fp, 33);//使文件指针移动到33字节位置
     print_file_pointer($fp);//打印此刻文件指针的位置
     
     rewind($fp);//使文件指针移动到0字节位置(初始位置)
     print_file_pointer($fp);//打印此刻文件指针的位置
$fclose($fp);
?>


Demo:

所以我们需要正确理解fgets(),fpassthru()这些函数的作用:

fgets():从当前文件指针的位置到本行结束的数据,而不是一定输出一整行

fpassthru():从当前文件指针的位置到全部内容结束的数据,而不是一定输出所有的数据

但在这里你可能会有疑问:为什么输出“湖湾”后的指针位置会是17而不是15呢?按理说输出“我叫彭湖湾”这5个汉字一共占3*5 = 15个字节,多出来的17 - 15 =2字节是什么呢?

多出来的两个字节是windows下的回车换行符\n\r

\n是换行,占一字节,\r是回车,占一字节,在六中我将会介绍

六.Windows和UNIX下的回车和换行


<?php
   $DOCUMENT_ROOT = $_SERVER[&#39;DOCUMENT_ROOT&#39;];
   $fp = fopen("$DOCUMENT_ROOT/text.txt",&#39;r&#39;);
   while(!feof($fp)){
    echo fgets($fp);
    echo ftell($fp);
   }
   fclose($fp);
?>


我们在windows下敲下回车键的时候,相当于键入了\n\r,所以“我叫彭湖湾”的15字节+“\n\r”的2字节 = 17字节

在mac下不一样的是:敲下回车键的时候,相当于只键入了\n,所以“我叫彭湖湾”的15字节+“\n”的1字节 = 16字节

相关推荐:

如何将python中文件写入TXT

php使用flock阻塞和非阻塞文件写入的实例讲解

PHP文件读写操作之文件写入代码

php 文件写入

The above is the detailed content of PHP file writing and reading graphics and text details. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn