Home  >  Article  >  Backend Development  >  Summary of PHP file operation methods, _PHP tutorial

Summary of PHP file operation methods, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:47:56933browse

Summary of PHP file operation methods,

Write data in the data file:

 <&#63;php
 /**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/29
 * Time: 17:05
 */
 header("Content-type: text/html; charset=utf-8");
 //write data
 $f = fopen('data','w');//打开文件
 fwrite($f,'Hello PHP');//写入数据
 fclose($f);//关闭文件
 echo 'OK';
 //windows环境暂时不考虑权限问题

After successful writing, you can see "OK" on the page

Next read the data in the data file

<&#63;php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/29
 * Time: 17:05
 */
header("Content-type: text/html; charset=utf-8");

//read data
$f = fopen('data','r');
$content = fgets($f);
echo $content;
fclose($f);

How to read if there are multiple rows of data?

Method 1 while:

<&#63;php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/29
 * Time: 17:05
 */
header("Content-type: text/html; charset=utf-8");
$f = fopen('data','r');
//读取多行数据 while
while(!feof($f)){//feof() 函数检测是否已到达文件末尾
  $content = fgets($f);
  echo $content;
}
fclose($f);

Method 2 file_get_contents():

echo file_get_contents('data');

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1024924.htmlTechArticleSummary of PHP file operation methods, write data in the data file: php /*** Created by PhpStorm. * User: Administrator * Date: 2015/6/29 * Time: 17:05*/ header ("Content-type: text/ht...
Statement:
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