Heim  >  Artikel  >  Backend-Entwicklung  >  php实现以只读方式打开文件的方法_PHP

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

WBOY
WBOYOriginal
2016-06-01 11:06:19873Durchsuche

本文实例讲述了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;>

上面的代码返回如下结果

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

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

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn