php实现以只读方式打开文件的方法


本文实例讲述了php实现以只读方式打开文件的方法。分享给大家供大家参考。具体分析如下:

php中可以通过fopen()打开一个文件,第二个参数设置为"r"表示已只读方式打开,函数返回一个文件句柄,其它函数就可以通过这个文件句柄对文件进行不同方式的读取

<?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()函数关闭文件句柄,以释放资源

希望本文所述对大家的php程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3