PHP文件处理—写入文件及操作文件

黄舟
Release: 2023-03-07 13:42:01
original
12363 people have browsed it

PHP文件处理—写入文件及操作文件

PHP文件处理中,有打开和关闭文件以及文件的读取,文件的读取也有读取一行,字符 ,字串以及整个文件,那么既然有读取,肯定就有写入,今天的这篇文章我们就给大家详解一下写入文件和操作文件!

我们刚刚说的在前三篇文章《PHP文件处理—打开/关闭文件》,《PHP文件处理—读取文件(一个字符,字串)》以及《PHP文件处理—如何读取文件(一行,整个文件)》中都有详细的介绍,没有阅读过的小伙伴,可以去看下!今天我们主要讲解写入文件和文件操作。

一:将数据写入文件

写入数据也是 PHP 中常用的文件操作,在PHP中使用 fweite() 和file_put_contents()函数向文件中写入数据。

1.fwrite()何时能电话也称之为fputs(),他们两个的用法是相同的。fwrite()函数语法如下:

int fwrite ( resource $handle , string $string [, int $length ] )
Copy after login

该函数把内容string 写入指针handle 所指向的文件。如果指定参数 length,那么写入length个字节后停止,如果文件内容长度小于 length,那么会输出全部文件内容。

2.file_put_contents()函数是PHP5新增的函数,该函数语法格式如下:

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
Copy after login
  • filename为要写入数据的文件

  • data为要写入的数据

  • flags 可以是FILE_USE_INCLUDE_PATH,FILE_APPEND或者 LOCK_EX,其中LOCK_EX为独占锁定,在下面的文章我们会详细介绍!

使用file_put_contents()函数和依次调用 fopen(),fwrite(),fclose()函数所实现的功能一样。下面我们通过具体实力比较一下函数的优越性!

本实例首先使用 fwrite()函数向文件写入数据,在使用file_put_contents()函数写入数据,具体示例代码如下:

用file_put_contents函数写入文件:";
file_put_contents($filepath,$str);
readfile($filepath);
?>
Copy after login

输出结果为:

24.png

二:操作文件

PHP 除了可以对文件内容进行读写,对文件本身同样也可以进行操作,如:复制,重命名,查看修改日期等。PHP 内置了大量的文件操作函数,常用的文件操作函数如下表:

函数原型函数说明举例
bool copy(string path1,string path2)将文件从path1复制到path2。如果成功则返回true,失败返回false。copy('tm.txt','../tm.txt')
bool rename(string filename1,string filename2)把name1重新命名为name2rename('1.txt','test.txt')
bool unlink(string filename)删除文件,成功返回true,失败返回false。unlink('tm.txt')
int fileatime(string filename)返回文件最后一次被访问的时间,以Linux时间戳的方式返回fileatime('test.txt')
int filemtime(string filename)返回文件最后一次被修改的时间,以Linux时间戳的方式返回date("Y-m-d 
H:i:s",filemtime("test.txt"))
int filesize(string filename)取得文件filename的大小
(bytes)
filesize('1.txt')
array pathinfo(string name[,int options])返回一个数组,包含文件的name的路径信息。有dirname,basename和extension。可通过option设置要返回的信息,有PATHINFO_DIRNAME、PATHINFO_BASENAME和PATHINFO_EXTENSION。默认为返回全部$arr=pathinfo('/tm/s1/16/4/9/1.txt');
foreach($arr as 
$method=>$value){
  
 echo $method.":".$value."
";}
string realpath(string filename)返回文件filename的绝对路径,如D:\wampserver\www\test\test.txtrealpath(test.txt)
array stat(string filename)返回一个数组,包括文件的相关信息,如上面提到的文件大小、最后修改时间等。$arr=stat('test.txt');
foreach($arr as 
$method=>$value){
      echo 
$method.":".$value."
";
}

注意:

在读写文件时,除了file(),readfile()等少数几个函数外,其他错做必须要先使用 fopen()函数打开文件,最后用 fclose()函数关闭文件,文件的信息函数(如:filesize,filemtime 等)则都不需要打开文件,只要文件存下就可以了!

PHP文件处理到这里就结束了,下面的文章我们将 PHP目录处理,具体内容请阅读《PHP目录处理—打开/关闭目录》!

The above is the detailed content of PHP文件处理—写入文件及操作文件. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!