fopen php 读取_PHP文件处理fopen 读写

fopen()是用来打开文件,在PHP 。

第一个参数的这一功能包含文件的名称将开放第二个参数指定的文件模式应打开:

<?php

$file=fopen("welcome.txt","r");

?>

The file may be opened in one of the following modes:

Modes Description

r Read only. Starts at the beginning of the file

r Read/Write. Starts at the beginning of the file

w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist

w Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist

a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist

a Read/Append. Preserves file content by writing to the end of the file

x Write only. Creates a new file. Returns FALSE and an error if file already exists

x Read/Write. Creates a new file. Returns FALSE and an error if file already exists

注意:如果fopen()函数是无法打开指定的文件,它返回0 (假) 。

例如

下面的例子生成一个讯息,如果fopen()函数是无法打开指定的文件:

  
<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!");

?>

  

fclose 关闭文件

该fclose ( )函数是用来关闭一个开放的档案

<?php $file = fopen("test.txt","r");
//some code to be executed
fclose($file);  ?>
检查文件结束

该feof ( )函数检查如果“文件结束” (的EOF )已经达成。

该feof ( )函数是有益的循环,通过数据的未知的长度。

注意:您不能读取档案开放瓦特,一个,和x模式

 
 
if (feof($file)) echo "End of file";
 
 
 

读文件一行行

该fgets ( )函数是用来读取一个单一的从一个文件。

注意:在调用此函数的文件指针已经移到下一行。

例如

下面的例子中读取文件一行行,直至到达文件:

<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!");  //Output a line of the file until the end is reached  while(!feof($file))    {    echo fgets($file). "
"; } fclose($file); ?>
 
 

读文件的性格特征

该fgetc ( )函数是用来读取一个字符从一个档案。

注意:在调用此函数的文件指针移动到下一个字符。

例如

下面的例子中读取一个文件性质的特点,到年底达成的文件是:

<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!");  while (!feof($file))     {     echo fgetc($file);     }  fclose($file);  ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值