【问题标题】:What's the importance and usage of additional file opening modes(modes with additional '+' sign) in PHP?PHP中附加文件打开模式(带有附加'+'符号的模式)的重要性和用法是什么?
【发布时间】:2015-07-29 00:46:30
【问题描述】:

我是 PHP 新手。

我正在阅读 PHP 的基本核心概念。 '文件处理'是PHP的基本和重要概念之一。

在研究这个概念时,我遇到了 PHP 中可用的各种文件打开模式。以下是每种文件打开方式的说明:

Modes   Description
  r        Open a file for read only. File pointer starts at the beginning of the file
  w        Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
  a        Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
  x        Creates a new file for write only. Returns FALSE and an error if file already exists
  r+       Open a file for read/write. File pointer starts at the beginning of the file
  w+       Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
  a+       Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
  x+       Creates a new file for read/write. Returns FALSE and an error if file already exists

我能够从上面的列表中理解前四种文件打开模式的目的(即“r”、“w”、“a”和“x”),但我完全无法理解上面列表中的其他文件打开模式(即“r+”、“w+”、“a+”和“x+”),因为它与前四种文件打开模式的描述相同。

所以我的问题是,既然前四种基本文件打开模式已经可用,那么为什么需要/定义这些额外的文件打开模式?使用它们的目的是什么?在处理文件时何时使用前四个以及何时使用后四个?

请帮助我解决我的上述疑问。 如果你能用一些合适的例子来解释,那对我和其他社区成员来说真的很棒并且很有帮助。

谢谢。

【问题讨论】:

  • 你看不出“只读/只写”和“读/写”有什么区别吗?
  • 我们在这个问题上有什么进展吗?

标签: php file file-io file-handling


【解决方案1】:

简而言之,“+”表示打开文件以供读写。

From the PHP doc:

'r' 以只读方式打开;将文件指针放在文件的开头。

'r+' 开放读写;将文件指针放在文件的开头。

'w' 只为写入而打开;将文件指针放在文件的开头并将文件截断为零长度。如果文件不存在,请尝试创建它。

'w+' 开放读写;将文件指针放在文件的开头并将文件截断为零长度。如果文件不存在,请尝试创建它。

'a' 只为书写而打开;将文件指针放在文件末尾。如果文件不存在,请尝试创建它。

'a+' 开放读写;将文件指针放在文件末尾。如果文件不存在,请尝试创建它。

'x' 创建并打开仅供写入;将文件指针放在文件的开头。如果文件已经存在,则 fopen() 调用将失败,返回 FALSE 并生成 E_WARNING 级别的错误。如果该文件不存在,请尝试创建它。这相当于为底层 open(2) 系统调用指定 O_EXCL|O_CREAT 标志。

'x+' 创建并打开读写;否则,它的行为与“x”相同。

'c' 打开文件仅用于写入。如果文件不存在,则创建它。如果它存在,它既不会被截断(与“w”相反),对该函数的调用也不会失败(就像“x”一样)。文件指针位于文件的开头。如果希望在尝试修改文件之前获得建议锁(请参阅flock()),这可能很有用,因为使用'w'可以在获得锁之前截断文件(如果需要截断,可以使用 ftruncate()在请求锁定后使用)。

'c+' 打开文件进行读写;否则它的行为与 'c' 相同。

【讨论】:

    【解决方案2】:

    让我们来看看所有这些模式,看看它们的作用:

    • 模式:r 使用模式 r,您只能读取存在的文件,这意味着如果文件不存在,则不会创建文件,也无法写入:

      1. 试图访问一个不存在的文件:

        $filename = "test.txt"; //file does not exists 
        $h = fopen($filename, "r");
        

      这将导致错误:

      警告:fopen(test.txt):打开流失败:没有这样的文件或目录

      因为文件不存在,也没有被创建。

      1. 尝试写入文件:

        $filename = "test.txt"; //file does exists
        $h = fopen($filename, "r");
        fwrite($h, "TEST");
        fclose($h);
        

      这不会给您带来任何错误,但如果您这样做,fwrite() 将返回 FALSE:var_dump(fwrite($h, "TEST"));,因为它无法写入模式为 r 的文件。

      1. 尝试读取文件:

        $filename = "test.txt"; //file does exists
        $h = fopen($filename, "r");
        $content = fread($h, filesize($filename));
        fclose($h);
        
        print_r($content);
        

      代码可以正常工作,您将从显示的文件中获取全部内容。

    • 模式:r+这个模式和r模式类似,但是有点不同,就是可以写入文件。

      1. 尝试写入文件:

        $filename = "test.txt"; //file does exists
        $h = fopen($filename, "r+");
        fwrite($h, "TEST");
        fclose($h);
        

      与之前的代码相同,但不同的是,var_dump(fwrite($h, "TEST")); 将返回它已写入文件的字节数(此处为 int(4))。

      另请注意,如果您在其中写入内容,它将覆盖文件。否则,模式 r+ 与模式 r 具有相同的行为,即读取文件并打开不存在的文件。

    • 模式:w 在此模式下,您只能写入文件,因此无法读取文件,如果文件不存在,它会尝试创建文件。与 r+ 一样,如果您写入文件,它将覆盖其中的所有内容。

      1. 试图访问一个不存在的文件:

        $filename = "test.txt"; //file does not exists 
        $h = fopen($filename, "w");
        fclose($h);
        

      所以这段代码可以正常工作,不会给你任何错误。由于该文件不存在,它只会尝试创建它。

      1. 尝试写入文件:

        $filename = "test.txt"; //file can exist or not, doesn't make a difference
        $h = fopen($filename, "w");
        fwrite($h, "TEST");
        fclose($h);
        

      此代码也可以正常工作,并将内容写入您的文件。正如我所说,它将覆盖文件!

      1. 尝试读取文件:

        $filename = "test.txt"; //file can exist or not, doesn't make a difference
        $h = fopen($filename, "w");
        $content = fread($h, filesize($filename));
        fclose($h);
        
        print_r($content);
        

      此代码最终会出现警告:

      警告:fread():长度参数必须大于0

      因为通过 w 模式访问文件,文件将被截断为零长度。所以你不能用 fread() 读任何东西。 (在 w+ 模式的例子中也有更多的介绍)

      ​​>
    • 模式:w+此模式与w模式类似,不同的是,你也可以读取文件。

      1. 尝试读取文件:

        $filename = "test.txt"; 
        $h = fopen($filename, "w+");
        fwrite($h, "TEST");
        fseek($h, 0); //See footnote 1
        $content = fread($h, filesize($filename));
        fclose($h);
        
        print_r($content);
        

      此代码可以正常工作并打印您:TEST。这里和w模式一样,会把文件截断为零长度,意思是打开文件后你必须往里面写点东西,然后才能读取。

      脚注:打开文件后它的长度为零,所以你必须在里面写一些东西,你才能读到一些东西。但是现在是这样,如果您打开文件,文件指针将位于文件的开头。然后当你写一些东西时,例如“XY”指针将位于 2,因此如果您尝试读取它,则从 2 开始 -> 然后您读取文件,因此显然没有内容,因此您必须使用 fseek 将文件指针设置回开头() 读取整个文件。

    • 模式:a:因此,使用模式 a,您可以将文本附加到文件中,但您无法读取它。如果文件不存在,它将尝试创建它。

      1. 试图访问一个不存在的文件:

        $filename = "test.txt"; //file does not exists 
        $h = fopen($filename, "a");
        fclose($h);
        

      这段代码的行为就像模式 w。如果文件不存在,它会尝试创建它。

      1. 尝试写入文件:

        $filename = "test.txt"; //file can exist or not, doesn't make a difference
        $h = fopen($filename, "a");
        fwrite($h, "TEST");
        fclose($h);
        

      此代码的工作方式类似于 w 模式,但不同之处在于,如果您的文件中已有内容,则不会被覆盖。

      1. 尝试读取文件:

        $filename = "test.txt"; //file can exist or not, doesn't make a difference
        $h = fopen($filename, "a");
        $content = fread($h, filesize($filename));
        fclose($h);
        
        print_r($content);
        

      这不会像 w 模式那样结束,您会收到警告,因为它在打开文件时不会覆盖文件。但是 fread() 也将返回 FALSE,因为您无法使用模式 a 读取文件。

    • 模式:a+而且这种模式和模式a类似,但不同的是,你也可以读取文件。

      1. 尝试读取文件:

        $filename = "test.txt"; 
        $h = fopen($filename, "a+");
        $content = fread($h, filesize($filename));
        fclose($h);
        
        print_r($content);
        

      如果文件中有内容,您将能够读取它并将其作为输出。

    • 模式:x 该模式是创建文件进行写入。如果文件已经存在,它会给你一个警告。

      1. 尝试创建文件:

        $filename = "test.txt";  //file does not exists
        $h = fopen($filename, "x");
        

      此代码将毫无问题地创建文件。如果它已经存在,你会得到一个错误。

      1. 尝试读取文件:

        $filename = "test.txt";  //file does not exists
        $h = fopen($filename, "x");
        $content = fread($h, filesize($filename));
        fclose($h);
        

      这将与此处的模式 w 具有相同的行为。

      1. 尝试写入文件:

        $filename = "test.txt";  //file does not exists
        $h = fopen($filename, "x");
        fwrite($h, "TEST");
        fclose($h);
        

      这也将具有与此处模式 w 相同的行为,您将能够写入文件。

    • 模式:x+ 如果您看到其他模式的模式,您已经猜到了,+ 也允许从文件中读取。

      1. 尝试读取文件:

        $filename = "test.txt";  //file does not exists
        $h = fopen($filename, "x+");
        fwrite($h, "TEST");
        fseek($h, 0); //See the footnote on mode w+
        $content = fread($h, filesize($filename));
        fclose($h);
        

      阅读将按预期正常工作。

    所以你可以看到这些模式都是不同的,并且做/允许不同的事情。这就是它们存在的原因,根据您的需要,您可以选择一种模式来打开文件。


    我希望这些示例清楚地向您展示所有这些模式之间的区别。如果您想了解有关模式和功能的更多信息,请参阅手册:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-12
      • 2017-07-18
      • 2021-09-30
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 2011-05-01
      • 2022-07-07
      相关资源
      最近更新 更多