php實現(xiàn)以只讀方式打開文件的方法

字號:


    這篇文章主要介紹了php實現(xiàn)以只讀方式打開文件的方法,實例分析了php中fopen函數(shù)的功能及使用技巧,需要的朋友可以參考下
    本文實例講述了php實現(xiàn)以只讀方式打開文件的方法。分享給大家供大家參考。具體分析如下:
    php中可以通過fopen()打開一個文件,第二個參數(shù)設置為"r"表示已只讀方式打開,函數(shù)返回一個文件句柄,其它函數(shù)就可以通過這個文件句柄對文件進行不同方式的讀取
    <?php
    $file = fopen("/tmp/file.txt", "r");
    print("Type of file handle: " . gettype($file) . "\n");
    print("The first line from the file handle: " . fgets($file));
    fclose($file);
    ?>
    上面的代碼返回如下結果
    Type of file handle: resource
    The first line from the file handle: Welcome to sharejs.com php tutorials
    文件讀取完成后,需要使用fclose()函數(shù)關閉文件句柄,以釋放資源
    希望本文所述對大家的php程序設計有所幫助。