php 文件读取和写入详细介绍例子
来源:互联网 发布:pr淘宝视频 编辑:程序博客网 时间:2024/06/06 12:34
*************介绍PHP文件的写入 和 读取**************
/**
*文件写入
*/
//覆盖写入
$filename = 'leyangjun.txt';$word = "北京欢迎你!";
$fh = fopen($filename, "w");
echo fwrite($fh, $word);
fclose($fh);
//追加写入
$filename = 'leyangjun.txt';
$word = "你好!";
$fh = fopen($filename, "a"); //参数选择 a ,则表示在文件后面追加写入:
echo fwrite($fh, $word);
fclose($fh);
//换行写入
$filename = 'leyangjun.txt';
$word = "北京欢迎你!\r\n"; // \r\n换行
$fh = fopen($filename, "a");
echo fwrite($fh, $word);
fclose($fh);
//写封装
$filename = 'leyangjun.txt';
$word = "北京欢迎你!\r\n";
// 确定文件存在并且可写
if (is_writable($filename)) {
//打开文件
if (!$fh = fopen($filename, 'a')) {
echo "不能打开文件 $filename";
exit;
}
// 写入内容
if (fwrite($fh, $word) === FALSE) {
echo "不能写入到文件 $filename";
exit;
}
$fh = fopen($filename, "a"); //参数选择 a ,则表示在文件后面追加写入:
fwrite($fh, $word);
fclose($fh);
} else {
echo "文件 $filename 不可写";
}
/**
*文件读取
*///读取文件内容,字符串输出
$myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("leyangjun.txt"));
fclose($myfile);
//fgets()函数用于从文件读取单行。
$myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
//推荐--->逐行读取(feof()函数检查是否已到达 "end-of-file",对于遍历未知长度的数据很有用)
$myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
// 输出单行直到 end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
//fgetc()函数用于从文件中读取单个字符。
$myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
// 输出单字符直到 end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
//你可以把你拿到的字符存储到你的逻辑中处理
$fp = fopen("leyangjun.txt", "r");
$arrData = array();
while(! feof($fp)){
$arrData[] = fgets($fp);
}
echo '<pre>';print_r($arrData);exit;
fclose($fp);
0 0
- php 文件读取和写入详细介绍例子
- 文件读取和写入
- 文件读取和写入
- 文件读取和写入
- php文件读取与写入
- PHP文件写入与读取
- PHP 大文件的读取和写入问题
- 读取文件和写入文件
- PHP读取文件函数/写入文件函数
- php操作文件(读取写入文件)
- javascript写入和读取cookies的例子
- 文件的读取和写入
- 读取和写入plist文件
- 写入和读取顺序文件
- 读取和写入plist文件
- ******************* 文件的写入和读取 *******************
- 【OpenMesh】文件读取和写入
- 文件的读取和写入
- HPU 1248: HH的军训【容斥原理】
- iOS开发之小文件下载、大文件下载、断点下载
- android中Scrollview中套ListView,高度超出屏幕,listview无法滑动问题
- 单例模式
- 数组中只出现一次的数(1)(C++)
- php 文件读取和写入详细介绍例子
- 学习JQuery的$.Ready()与OnLoad事件比较[转]
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter详解
- 开灯问题
- 用AndroidStudio开发cocos2d-x 项目
- [delve][web]如何实现网站顶部状态条的外部引用
- JS中格式化日期
- canvas 学习(一)
- c#的构造函数及构造函数的调用