>  기사  >  백엔드 개발  >  PHP는 파일을 읽고 쓰기

PHP는 파일을 읽고 쓰기

WBOY
WBOY원래의
2016-08-08 09:24:551130검색

파일 정보 가져오기:

//打开文件
$file_path = "test.txt";

//获取文件信息

if($fp=fopen($file_path,"r")){
	$file_info = fstat($fp);
	echo "<pre class="brush:php;toolbar:false">";
	print_r($file_info);
	echo "
"; echo "
文件大小是{$file_info['size']}"; echo "
文件上次修改时间".date("Y-m-d H:i:s",$file_info['mtime']); echo "
文件上次访问时间".date("Y-m-d H:i:s",$file_info['atime']); echo "
文件上次change时间".date("Y-m-d H:i:s",$file_info['ctime']); }else{ echo "打开文件失败!"; } //关闭文件 fclose($fp); ".filesize($file_path); echo "
".date("Y-m-d H:i:s",fileatime($file_path)); echo "
".filectime($file_path); echo "
".filemtime($file_path); ?>

파일 정보 읽기:

<?php
//读取文件
$file_path = "test.txt";
//先判断文件是否存在
if(file_exists($file_path)){
	$fp = fopen($file_path,"a+");
	//读取内容并且输出
	$content = fread($fp,filesize($file_path));
	echo "文件内容是:<br/>";
	$content = str_replace("\r\n", "<br/>", $content);
	echo $content;
}else{
	echo "文件不存在";
}
//关闭文件
fclose($fp);
?>

파일 정보 읽기 간단한 방법:

<span style="font-size:18px;"><?php
// //读取文件
$file_path = "test.txt";
$contents = file_get_contents($file_path);
$contents = str_replace("\r\n", "<br/>", $contents);
echo $contents;
?></span>


위 내용은 내용적인 측면을 포함하여 PHP 파일 읽기 및 쓰기를 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

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