Home  >  Article  >  php教程  >  php中常用文件操作函数介绍

php中常用文件操作函数介绍

WBOY
WBOYOriginal
2016-06-13 10:16:04822browse

小编今天来给php初学者介绍php文件操作的常用函数使用方法总结,包括:文件读写,创建,查看文件属性,文件删除等等关于文件的操作。

在对一个文件进行访问之前,一般我们都需要判断文件是否存在,以免调用了不存在的文件导致错误。

php判断文件是否存在函数:file_exists(),结构形式如下:

file_exist($string);

参数$string为一个指向文件或目录的字符型变量,如果文件或目录存在,则返回true,否则返回false。

实例:

 代码如下 复制代码

 /* 判断post.php是否存在 */
 $file="post.php";
 if(file_exists($file)){
  echo "文件存在
";
 }
 else{
  echo "文件不存在
";
 }
 
 /* 判断images目录是否存在 */
 $category="images";
 if(file_exists($category)){
  echo "目录存在";
 }
 else{
  echo "目录不存在";
 } 
?>

php提供了一些访问文件属性的函数,可以获取文件的大小、类型、修改时间等

Statement:
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 admin@php.cn