PHP | fclose()函数

PHP中的fclose()函数是一个内置函数,用于关闭由打开的文件指针指向的文件。函数的作用是:成功时返回true,失败时返回false。它将文件作为必须关闭的参数,并关闭该文件。

null

语法:

bool fclose( $file )

参数: PHP中的fclose()函数只接受一个参数$file。此参数指定必须关闭的文件。

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

错误和例外 :

  1. 如果文件是通过fwrite()函数写入的,并且必须读取文件的内容,则必须先使用fclose()函数关闭文件。
  2. PHP中的fclose()函数不适用于远程文件。它只适用于服务器文件系统可以访问的文件。

    例如:

    Input : $check = fopen("gfg.txt", "r");
            fclose($check);
    Output : true
    
    Input: $check = fopen("singleline.txt", "r");
           $seq = fgets($check);
           while(! feof($check))
           {
             echo $seq ;
             $seq = fgets($check);
           }
           fclose($check);
    Output:true
    

    下面的程序演示了fclose()函数:

    方案1 :

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

    
    

    输出:

    true

    方案2 :在下面的程序中,名为 单线。txt 仅包含一行“此文件仅包含一行”。

    <?php
    // a file is opened using fopen() function
    $check = fopen ( "singleline.txt" , "r" );
    $seq = fgets ( $check );
    // Outputs a line of the file until
    // the end-of-file is reached
    while (! feof ( $check ))
    {
    echo $seq ;
    $seq = fgets ( $check );
    }
    // the file is closed using fclose() function
    fclose( $check );
    ?>

    
    

    输出:

    This file consists of only a single line.
    

    参考 : http://php.net/manual/en/function.fclose.php

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享