search
Homephp教程php手册php文件操作相关类实例

这篇文章主要介绍了php文件操作相关类,实例分析了php针对文件与目录的创建、删除、复制、检查等操作技巧,需要的朋友可以参考下

本文实例讲述了php文件操作相关类。分享给大家供大家参考。具体如下:

check_exist($dirname) and is_dir($dirname)) { if(!$dirhandle=opendir($dirname)) return false; while(($file=readdir($dirhandle))!==false) { if($file=="." or $file=="..") continue; $file=$dirname.DIRECTORY_SEPARATOR.$file; //表示$file是$dir的子目录 if(is_dir($file)) { $this->delete_dir($file); } else { unlink($file); } } closedir($dirhandle); return rmdir($dirname); } else return false; } function copy_dir($dirfrom,$dirto) //复制目录 { if(!is_dir($dirfrom)) return false; if(!is_dir($dirto)) mkdir($dirto); $dirhandle=opendir($dirfrom); if($dirhandle) { while(false!==($file=readdir($dirhandle))) { if($file=="." or $file=="..") continue; $filefrom=$dirfrom.DIRECTORY_SEPARATOR.$file; //表示$file是$dir的子目录 $fileto=$dirto.DIRECTORY_SEPARATOR.$file; if(is_dir($filefrom)) { $this->copy_dir($filefrom,$fileto); } else { if(!file_exists($fileto)) copy($filefrom,$fileto); } } } closedir($dirhandle); } function getdir_size($dirname) //获取目录大小 { if(!file_exists($dirname) or !is_dir($dirname)) return false; if(!$handle=opendir($dirname)) return false; $size=0; while(false!==($file=readdir($handle))) { if($file=="." or $file=="..") continue; $file=$dirname."http://www.jb51.net/".$file; if(is_dir($file)) { $size+=$this->getdir_size($file); } else { $size+=filesize($file); } } closedir($handle); return $size; } function getReal_size($size) // 单位自动转换函数 { $kb=1024; $mb=$kb*1024; $gb=$mb*1024; $tb=$gb*1024; if($size=$kb and $size=$mb and $size=$gb and $size=$tb) return round($size/$tb,2)."TB"; } function copy_file($srcfile,$dstfile) { if(is_file($srcfile)) { if(!file_exists($dstfile)) return copy($srcfile,$dstfile); } else return false; } function unlink_file($filename) //删除文件 { if($this->check_exist($filename) and is_file($filename)) { return unlink($filename); } else return false; } function getsuffix($filename) //获取文件名后缀 { if(file_exists($filename) and is_file($filename)) { return end(explode(".",$filename)); } } function input_content($filename,$str) //将字符串写入文件 { if(function_exists(file_put_contents)) { file_put_contents($filename,$str); } else { $fp=fopen($filename,"wb"); fwrite($fp,$str); fclose($fp); } } function output_content($filename) //将整个文件内容读出到一个字符串中 { if(function_exists(file_get_contents)) { return file_get_contents($filename); } else { $fp=fopen($filename,"rb"); $str=fread($fp,filesize($filename)); fclose($fp); return $str; } } function output_to_array($filename) //将文件内容读出到一个数组中 { $file=file($filename); $arr=array(); foreach($file as $value) { $arr[]=trim($value); } return $arr; } } //$dir=new file_dir; //$size=$dir->getdir_size("F:/wamp/www/class/images"); //echo $dir->getReal_size($size); ?>

希望本文所述对大家的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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool