时间:2021-07-01 10:21:17 帮助过:22人阅读
- //打开文件
- $file_path = "test.txt";
- //获取文件信息
- if($fp=fopen($file_path,"r")){
- $file_info = fstat($fp);
- echo "<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>";</li><li>print_r($file_info);</li><li>echo "</li></ol></pre>";
- echo "<br>文件大小是{$file_info['size']}";
- echo "<br>文件上次修改时间".date("Y-m-d H:i:s",$file_info['mtime']);
- echo "<br>文件上次访问时间".date("Y-m-d H:i:s",$file_info['atime']);
- echo "<br>文件上次change时间".date("Y-m-d H:i:s",$file_info['ctime']);
- }else{
- echo "打开文件失败!";
- }
- //关闭文件
- fclose($fp);
- <!--?php//第二种方式获取文件信息
- echo "<br/-->".filesize($file_path);
- echo "<br>".date("Y-m-d H:i:s",fileatime($file_path));
- echo "<br>".filectime($file_path);
- echo "<br>".filemtime($file_path);
- ?>
读取文件信息:
- <!--?php
- //读取文件
- $file_path = "test.txt";
- //先判断文件是否存在
- if(file_exists($file_path)){
- $fp = fopen($file_path,"a+");
- //读取内容并且</pre-->输出
- $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教程有兴趣的朋友有所帮助。