今天与大家分享一个PHP文件操作类,进行常规的FSO文件操作,实现的功能有:PHP获取指定文件大小、判断文件及目录是否存在、删除文件或目录、复制文件或目录、同时创建多级目录、读取文件内容到字符串中、将字符串写入文件、获取文件名后缀、获取目录大小等,使用的PHP函数有:file_exists()、mkdir()、filesize、copy、unlink等,这些知识对熟悉PHP中的文件系统操作,也就是FSO相当有用了:
006 | function check_exist( $filename ) |
008 | if ( file_exists ( $filename )) |
014 | function create_dir( $dirname , $mode =0777) |
016 | if ( is_null ( $dirname ) || $dirname == "" ) return false; |
017 | if (! is_dir ( $dirname )) |
019 | return mkdir ( $dirname , $mode ); |
023 | function createDir( $aimUrl ) |
025 | $aimUrl = str_replace ( '\\' , '/' , $aimUrl ); |
027 | $arr = explode ( '/' , $aimUrl ); |
028 | foreach ( $arr as $str ) |
030 | $aimDir .= $str . '/' ; |
031 | if (! file_exists ( $aimDir )) |
038 | function delete_dir( $dirname ) |
040 | if ( $this ->check_exist( $dirname ) and is_dir ( $dirname )) |
042 | if (! $dirhandle =opendir( $dirname )) return false; |
043 | while (( $file =readdir( $dirhandle ))!==false) |
045 | if ( $file == "." or $file == ".." ) continue ; |
046 | $file = $dirname .DIRECTORY_SEPARATOR. $file ; |
049 | $this ->delete_dir( $file ); |
054 | closedir ( $dirhandle ); |
055 | return rmdir ( $dirname ); |
060 | function copy_dir( $dirfrom , $dirto ) |
062 | if (! is_dir ( $dirfrom )) return false; |
063 | if (! is_dir ( $dirto )) mkdir ( $dirto ); |
064 | $dirhandle =opendir( $dirfrom ); |
067 | while (false!==( $file =readdir( $dirhandle ))) |
069 | if ( $file == "." or $file == ".." ) continue ; |
070 | $filefrom = $dirfrom .DIRECTORY_SEPARATOR. $file ; |
071 | $fileto = $dirto .DIRECTORY_SEPARATOR. $file ; |
072 | if ( is_dir ( $filefrom )) |
074 | $this ->copy_dir( $filefrom , $fileto ); |
077 | { if (! file_exists ( $fileto )) |
078 | copy ( $filefrom , $fileto ); |
082 | closedir ( $dirhandle ); |
085 | function getdir_size( $dirname ) |
087 | if (! file_exists ( $dirname ) or ! is_dir ( $dirname )) return false; |
088 | if (! $handle =opendir( $dirname )) return false; |
090 | while (false!==( $file =readdir( $handle ))) |
092 | if ( $file == "." or $file == ".." ) continue ; |
093 | $file = $dirname . "/" . $file ; |
096 | $size += $this ->getdir_size( $file ); |
098 | $size += filesize ( $file ); |
106 | function getReal_size( $size ) |
112 | if ( $size < $kb ) return $size . "B" ; |
113 | if ( $size >= $kb and $size < $mb ) return round ( $size / $kb ,2). "KB" ; |
114 | if ( $size >= $mb and $size < $gb ) return round ( $size / $mb ,2). "MB" ; |
115 | if ( $size >= $gb and $size < $tb ) return round ( $size / $gb ,2). "GB" ; |
116 | if ( $size >= $tb ) return round ( $size / $tb ,2). "TB" ; |
119 | function copy_file( $srcfile , $dstfile ) |
121 | if ( is_file ( $srcfile )) |
123 | if (! file_exists ( $dstfile )) |
124 | return copy ( $srcfile , $dstfile ); |
129 | function unlink_file( $filename ) |
131 | if ( $this ->check_exist( $filename ) and is_file ( $filename )) |
133 | return unlink( $filename ); |
137 | function getsuffix( $filename ) |
139 | if ( file_exists ( $filename ) and is_file ( $filename )) |
141 | return end ( explode ( "." , $filename )); |
145 | function input_content( $filename , $str ) |
147 | if (function_exists( file_put_contents )) |
149 | file_put_contents ( $filename , $str ); |
151 | $fp = fopen ( $filename , "wb" ); |
157 | function output_content( $filename ) |
159 | if (function_exists( file_get_contents )) |
161 | return file_get_contents ( $filename ); |
163 | $fp = fopen ( $filename , "rb" ); |
164 | $str = fread ( $fp , filesize ( $filename )); |
170 | function output_to_array( $filename ) |
172 | $file =file( $filename ); |
174 | foreach ( $file as $value ) |
上述文件可保存为file_dir.class.php,使用方法如下:
1 | include 'file_dir.class.php' ; |
3 | $size = $dir ->getdir_size( "D:/wamp/www/class/images" ); |
4 | echo $dir ->getReal_size( $size ); |