Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 09:12:09
Original
1033 people have browsed it

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

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

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

<&#63;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); 
&#63;>
Copy after login

上面的代码返回如下结果

Type of file handle: resource
The first line from the file handle: Welcome to sharejs.com php tutorials

文件读取完成后,需要使用fclose()函数关闭文件句柄,以释放资源

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!