当前位置:  首页>> 技术小册>> PHP合辑2-高级进阶

PHP中的fclose()函数是一个内建函数,用于关闭由打开的文件指针所指向的文件。fclose()函数在成功时返回true,在失败时返回false。它接受要关闭的文件作为参数,并关闭该文件。

语法:

  1. bool fclose( $file )

参数:PHP中的fclose()函数只接受一个参数,即$file。该参数指定要关闭的文件。

返回值:在成功时返回true,在失败时返回false。

错误和异常:

如果已经通过fwrite()函数写入文件,必须首先使用fclose()函数关闭文件,才能读取文件的内容。

PHP中的fclose()函数不适用于远程文件。它仅适用于服务器文件系统可访问的文件。

示例:

  1. Input : $check = fopen("gfg.txt", "r");
  2. fclose($check);
  3. Output : true
  4. Input: $check = fopen("singleline.txt", "r");
  5. $seq = fgets($check);
  6. while(! feof($check))
  7. {
  8. echo $seq ;
  9. $seq = fgets($check);
  10. }
  11. fclose($check);
  12. Output:true

示例1

  1. <?php
  2. // opening a file using fopen() function
  3. $check = fopen("gfg.txt", "r");
  4. // closing a file using fclose() function
  5. fclose($check);
  6. ?>

output:

  1. true

示例2

  1. <?php
  2. // a file is opened using fopen() function
  3. $check = fopen("singleline.txt", "r");
  4. $seq = fgets($check);
  5. // Outputs a line of the file until
  6. // the end-of-file is reached
  7. while(! feof($check))
  8. {
  9. echo $seq ;
  10. $seq = fgets($check);
  11. }
  12. // the file is closed using fclose() function
  13. fclose($check);
  14. ?>

output

  1. This file consists of only a single line.