Home  >  Article  >  Backend Development  >  Detailed introduction to PHP file operations (1)

Detailed introduction to PHP file operations (1)

黄舟
黄舟Original
2017-03-20 09:22:461265browse

Judge file type

var_dump(filetype("./img")); //返回文件类型,目录或文件
var_dump(is_dir("./img/11.png")); //判断给的文件是不是目录
is_file(); //判断给的文件是不是文件

File attributes

var_dump(date("Y-m-d H:i:s",fileatime("./img/11.png"))); //文件上次访问时间
var_dump(date("Y-m-d H:i:s",filemtime("./img/11.png"))); //文件修改时间
echo filesize("./img/11.png"); //文件大小
filectime("") //文件创建时间
file_exists("") //文件是否存在

File path

/代表根  网页里面代表www目录   php里面代表磁盘根 
var_dump(file_exists("/wamp/www/1220/wenjian/img/11.png")); //文件是否存在
echo $_SERVER['DOCUMENT_ROOT']; //取服务器根路径
echo basename("/wamp/www/1220/wenjian/img/11.png"); //路径中获取文件名
echo dirname("/wamp/www/1220/wenjian/img/11.png"); //路径中目录名
var_dump(pathinfo("/wamp/www/1220/wenjian/img/11.png")); //返回数组
echo realpath("./img/11.png");//将相对路径转化为绝对路径

Traverse directory

var_dump(glob("./ajax/*")); //返回该目录下所有文件数组

Give me a folder and return the number of all files in the folder

function ShuLiang($url)
{
   $sl = 0;
   
   $arr = glob($url);
   foreach($arr as $v)
   {
      if(is_file($v))
      {
         $sl++;
      }
      else
      {
         $sl += ShuLiang($v."/*");
      }
   }
   
   
   return $sl;
}
echo ShuLiang("./ajax/*");

The above is the detailed content of Detailed introduction to PHP file operations (1). For more information, please follow other related articles on the PHP Chinese website!

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