当前位置: 首页 > 后端技术 > PHP

PHP文件系统

时间:2023-03-29 19:35:43 PHP

一、文件类型1、PHP只对服务器端的文件进行操作,具体操作函数与操作系统中一致。2、如果程序需要的数据和程序运行的结果需要长期保存,则必须以文件的形式保存。3、UNIX文件类型:block/char/dir/fifo/file/link/unknown;Windows文件类型:文件/目录/未知4。获取文件类型:filetype('./xxx.php')5.判断文件是否为某种类型:is_dir('./xxx')/is_file等二、文件属性1、获取文件的各个时间:fileatime/filemtime2、检查文件是否存在:file_exists3.获取文件大小:filesize4。示例:获取文件属性函数functiongetFilePro($path){if(file_exists($path)){//获取文件类型getFileType($path);//获取文件大小echogetFileSize($path).'
';//获取文件权限if(is_readable($path)){echo'文件可读
';}if(is_writable($path)){echo'文件可写
';}if(is_executable($path)){echo'文件可执行
>';}date_default_timezone_set('PRC');//获取文件创建时间echodate('y-m-dh:i:s',filectime($path)).'
';//获取文件访问时间echodate('y-m-dh:i:s',fileatime($path)).'
';//获取文件修改时间echodate('y-m-dh:i:s',filemtime($path)).'
';}else{echo'该文件不存在
';}};函数getFileSize($path){$unit='';$大小=文件大小($路径);如果($size>pow(2,40)){$unit=$size/pow(2,40).'TB';}elseif($size>pow(2,30)){$unit=$size/pow(2,30).'GB';}elseif($size>pow(2,20)){$unit=$size/pow(2,20).'MB';}elseif($size>pow(2,10)){$unit=$size/pow(2,10).'KB';}else{$unit=$size.'types';}返回$单位;};functiongetFileType($path){switch(fileType($path)){case'dir':echo"文件类型是目录
";休息;case'file':echo"文件类型为文件
";休息;case'unknown':echo"文件类型为其他
";休息;默认值:echo'未知
';}};getFilePro('123.txt');