>  기사  >  백엔드 개발  >  PHP 파일 작업 방법_php 스킬 요약

PHP 파일 작업 방법_php 스킬 요약

WBOY
WBOY원래의
2016-05-16 20:12:261194검색

데이터 파일에 데이터 쓰기:

 <&#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环境暂时不考虑权限问题

작성이 성공하면 페이지에 "OK"가 표시됩니다

다음으로 데이터 파일의 데이터를 읽습니다

<&#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);

데이터 행이 여러 개인 경우 어떻게 읽나요?

방법 1 동안:

<&#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);

방법 2 file_get_contents():

echo file_get_contents('data');

위 내용은 이 글의 전체 내용입니다. 모두 마음에 드셨으면 좋겠습니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.