php处理文件的方式与C语言类似。
使用函数fopen()打开一个文件,函数有两个参数,第一个指明文件,第二个指明打开的方式。方式有r只读模式;r+可读可写方式;w只写方式,如果文件不存在,就创建;w+可写可读方式,如果文件就存在,就创建;a 追加方式 , 以只写方式打开文件,把文件指针指向文件末尾处,如果该文件不存在,则建立该文件;a+ 追加 , 以可读可写方式打开文件,把文件指针指向文件末尾处,如果该文件不存在,则创建;b 二进制, 用于于其他模式进行连接。
如果文件打开成功返回该文件的指针,如果失败返回false。打开文件之后就需要对文件进行读取,fgets、fgetc、readfile都可以读取文件。fgets一次读取一行,fgetc一次读取一个字符,readfile一次读取整个文件。
php用fwrite来写文件,参数有两个,第一个指明文件的指针,第二部指明要写入的数据。
<html>
<body>
<?php
$file = fopen ("yeetrack.com/tem.txt","r");//只读方式
while(!feof($file))
{
echo fgets($file). "
";
}
fclose($file);
echo " ";
$file = fopen("yeetrack.com/tem.txt","r");
while(!feof($file))
{
echo fgetc($file);
}
fclose($file);
$file = fopen("yeetrack.com/tem.txt", "a+");//追加可读方式
$content = "hello world!";
echo $content;
fwrite($file,$content);
fclose($file);
echo "";
$file = fopen("yeetrack.com/tem.txt","r");
while(!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
</body>
</html>
<?php
/**
* @author youthflies
*/
//以只写方法打开tem.txt文件
$fileHandle = fopen("/home/youthflies/work/workspace/php/test/tem.txt","w") or die("打开文件失败!");
//fwrite()函数向tem.txt中写入文本
fwrite($fileHandle, "Hello world");
fclose($fileHandle);
/**
* 还可以使用file_put_contents()函数向文件中写入文本
* 该函数相当于依次调用了fopen()、fwrite()和fclose()函数
* 该方法是以w的方法打开文件,故是覆盖性写入
*/
file_put_contents("tem.txt", "Hello world, again!");
/**
* 读取文件内容
* fread()读取文件指定长度的内容
* file_get_contents()将文件内容读入字符串
* fgets()从打开的文件中最多读取一行文本
* fgetc()从打开的文件中读取一个字符
* file()把文件读入到数组中
* readfile()把文件读入到缓冲区中
* EOF表示文件结尾,php中提供feof()函数来判断文件指针是否到了末尾
**/
file_put_contents("tem.txt", "1hellon2worldn3again");
$handle = fopen("tem.txt", "r") or die("文件打开失败");
$string = fread($handle, 4);//从tem.txt中读取4个字节,即"1hel"
echo $string . "<br />";
fclose($handle);
$handle = fopen("tem.txt", "r") or die("文件打开失败");
//一次性将文件全部读出
$string = fread($handle, filesize("tem.txt"));
echo $string . "<br />";
fclose($handle);
//使用file_get_contents()函数将文件内容全部读入字符串,推荐使用这种方法
$string = file_get_contents("tem.txt");
echo $string . "<br />";
//使用fgets()函数一次从文件中最多读取一行文本,也可以指定读取的大小,默认大小为1024个字符
$handle = fopen("tem.txt", "r") or die("文件打开失败");
$string = fgets($handle);
echo $string . "<br />";
fclose($handle);
//与fgets()类似,fgetc()一次读取一个字符
$handle = fopen("tem.txt", "r") or die("文件打开失败");
while(($char=fgets($handle))!=false)
echo $char;
echo "<br />";
fclose($handle);
//函数file()不需要fopen()打开文件,它将整个文件读取到数组中,每个元素对应文件中的一行
print_r(file("tem.txt"));
echo "<br />";
//readfile()函数可以读取整个文件,并且立刻输出到缓冲区,返回读取的字节数,同样不需要fopen文件
readfile("tem.txt"); //直接输出到屏幕
//读取远程文件
$remoteFile = fopen("http://www.baidu.com/", "r") or die ("打开远程文件失败");
while(!feof($remoteFile))
echo fgets($remoteFile, 1024);
fclose($remoteFile);
//当然也可以读写ftp服务器上的文件,读写远程文件,可以网络原因导致超时,可以使用set_time_limit()函数设置超时时间
$remoteFile = fopen("ftp://username:password@ftp.server.com/", "w+") or die ("打开远程文件失败");
while(!feof($remoteFile))
echo fgets($remoteFile, 1024);
fclose($remoteFile);
?>
下面是对文件指针进行操作的几个函数:
$file1=fopen("docu.txt", "r") or die(" 文件打开失败");
//输出文件指针的位置
echo "文件指针:" . ftell($file1) ."<br />";
//读取10个字节
echo fread($file1, 10) . "<br />";
//输出文件指针的位置
echo "文件指针:" . ftell($file1) . "<br />";
//将文件指针向后移动14个字节(由于utf-8编码中,汉字占3个字节,可能会出现半个汉字乱码的情况)
fseek($file1, 14, SEEK_CUR);
echo "文件指针:" . ftell($file1) . "<br />";
echo fread($file1, 10) . "<br />";
//rewind()函数可以将文件指针复位
rewind($file1);
echo "文件指针:" . ftell($file1) . "<br />";
fclose($file1);
版权声明
本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。
© 空空博客,本文链接:https://www.yeetrack.com/?p=72
近期评论