php 常用文件操作学习笔记
1、获取文件名:basename();
2、获取文件所在的目录:dirname();
3、pathinfo()获取文件信息,返回结果为一个array,包括路径、文件全名、文件名和扩展名。例如:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7386')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7386>$file = '/com/netingcn/error.log';
print_r(pathinfo($file));
结果为:
Array(
[dirname] => /com/netingcn
[basename] => error.log
[extension] => log
[filename] => error
)
4、判断文件是否存在:is_file();
5、判断目录是否存在:is_dir();
6、判断文件或目录是否存在:file_exists();
7、读取文件所有内容:file()或file_get_contents(),其中file()返回的是一个一行为元素的array,file_get_contents()把文件全部内容作为一个String返回;
8、写文件fwrite,如:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5365')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5365>$handler = fopen($file, 'w'); // w 会冲掉以前的内容、a 是追加
fwrite($handler, 'content');
fclose($handler); //记得关闭打开的文件句柄9、文件读取操作有很多,下面简单介绍几个:
$handler = fopen($file, 'r');
while(!feof($handler)) {
$datas[] = fgets($handler); //读取一行内容
}
while(!feof($handler)) {
$datas[] = fgetss($handler); //读取一行内容并过来html标记
}
while(!feof($handler)) {
$datas[] = fgetcsv($handler); //读取一行内容并解析csv字段
}
$content = fread($handler, $strLength); //读取指定长读的字符
fclose($handler);
PHP 读文件内容,此文件为txt,路径$filePath为'list.txt', 即放在和php文件同一个目录,
注意:
本人的txt的内容样式如下:
Email,FirstName,LastName...
neil@ddd.com,neil,zhou...
...
具体实现根据自身情况修改。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1880')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1880> $fp = fopen($filePath,"r ");
$a = read_content_to_array($fp); // 取得文本文件中的内容
fclose($fp);
/**
@desc read file content to array
@params $fp: file resource
@return array
*/
function read_content_to_array($fp){
$i=0;
$a = array();
while (!feof ($fp)) { //while循环,条件:没到文件的末端。
$buffer = fgets($fp, MAX_BYTES_PER_ROW); //fgets() 函数用于从文件中逐行读取文件,,读取的字节数。默认是 1024 字节。MAX_BYTES_PER_ROW为自定义的一个常量,读取的字节数
$block=explode(STR_TD_DIVIDER,$buffer); //用分隔符对操作的一行($buffer)进行分割,返回由字符串组成的数组。STR_TD_DIVIDER为自定义的一个常量,每行中各个字段之间的分隔符。
$a[$i]=$block;
$i=$i 1; //通过while循环,带动i值递增,实现数组构建。
}
return $a;
}
PHP 写内容到txt文件中
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1426')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1426>$handle = fopen($filePath, "w ");
fwrite($handle,$str);
fclose($handle);
怎么样,是不是看起来很简单呢?下面是一些简单知识的总结:
fopen()
fopen() 函数用于在 PHP 中打开文件,此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪种模式来打开文件.
模式 描述
r 只读。在文件的开头开始。
r 读/写。在文件的开头开始。
w 只写。打开并清空文件的内容;如果文件不存在,则创建新文件。
w 读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。
a 追加。打开并向文件文件的末端进行写操作,如果文件不存在,则创建新文件。
a 读/追加。通过向文件末端写内容,来保持文件内容。
x 只写。创建新文件。如果文件以存在,则返回 FALSE。
x 读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。
注释:如果 fopen() 无法打开指定文件,则返回 0 (false)。
feof()
检测 End-of-file
feof() 函数检测是否已达到文件的末端 (EOF),在循环遍历未知长度的数据时,feof() 函数很有用。注释:在 w 、a 以及 x 模式,您无法读取打开的文件!
if (feof($file)) echo "End of file";
fgets()
fgets() 函数用于从文件中逐行读取文件。在调用该函数之后,文件指针会移动到下一行。
您可能感兴趣的文章:
golang学习笔记(一):包,变量,函数
零基础学python需要多久
Laravel 的 Events 及 Observers 模型事件学习笔记
mysql数据库基本操作学习笔记
PHP入门基础之常量类型学习笔记
php入门实例查询数据库记录
php有哪些简单的系统可以做
photoshop设计父亲节主题粉笔画效果制作教程
【PHP】xml学习笔记之增删改查(一)增加
笔记本电脑噪音问题解决办法