-
- /**
- * ?? ???? ?? ???
- * ???: bbs.it-home.org
- * ?:
- * $fileutil = new fileDirUtil()
- * $fileutil->createDir( 'a/1/2/3'); ?? ?? ??? ?? ?? a/1/2/3
- * $fileutil->createFile('b/1/2/3') ?? ??? b/1/2/ ?? ?? 3? ??
- * $fileutil->createFile('b/1/2/3.txt'); b/1/2/? ?? ?? ?? ??? file
- * $fileutil->writeFile('b/1/2/3.txt','this is what i write!') ?? ??? 3.exe ??? ?????.
- * $arr = $fileutil->readFile2array('example/mysql.txt')
- * $arr = $fileutil->readsFile('example/mysql.txt')
- * $ size=$ fileutil->bitSize($fileutil->getDirSize("example")); ?? ?? ???? ?? ????
- * $fileutil->copyDir('b','d/e' ); ?? ?? ??? ??? d/e ??? ??? b ??? ??? ??? ?????
- * $fileutil->copyFile('b/1/2/3.exe','b/ b/3.exe' ); ?? ??? ????? b/b ??? ??? b/1/2 ??? ?? 3.exe ??? ?? ??? ?????.
- * $fileutil->moveDir('a/' ,'b/c'); ?? ??? ????? b/c ??? ???? a ??? ??? ?? ??? ??? ? a ??? ?????
- * $fileutil->moveFile('b/1/ 2/3 .exe','b/d/3.exe'); ?? ??? ????? b/d ??? ??? b/1/2? 3.exe? ?? ??? ?????.
- * $fileutil->unlinkFile ('b/d/3.exe'); b/d/3.exe ??? ????? ??????
- * $fileutil->unlinkDir('d'); d ?? ??
- * $list = $fileutil->dirList("E:example"); ??? ?? ???? ????? ?? ??? ?????.
- * $list = $fileutil->dirTree("/ "); ??? ?? ?? ??? ????? ?? ?? ??? ???? ?? ??? ?????
- */
- class fileDirUtil {
- /**
- * ?? ???
- *
- * @param string $aimUrl
- * @return viod
- */
- function createDir($aimUrl, $mode = 0777) {
- $aimUrl = str_replace ( '', '/', $aimUrl );
- $aimDir = '';
- $arr = ??( '/', $aimUrl );
- foreach ( $arr as $str ) {
- $aimDir .= $str . '/';
- if (!file_exists ( $aimDir )) {
- mkdir ( $aimDir, $mode );
- }
- }
- }
- /**
- * ?? ??
- *
- * @param string $aimUrl
- * @param boolean $overWrite ? ????? ?? ??? ???? ??? ?????.
- * @return boolean
- */
- function createFile($aimUrl, $overWrite = false) {
- if (file_exists ( $aimUrl ) && $ overWrite == false) {
- return false;
- } elseif (file_exists ( $aimUrl ) && $overWrite == true) {
- $this->unlinkFile ( $aimUrl );
- }
- $aimDir = ???? ??( $aimUrl );
- $this->createDir( $aimDir );
- ??( $aimUrl );
- true? ?????.
- }
- /**
- * ?? ??
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite ? ????? ?? ??? ???? ??? ?????.
- * @ ?? ?? ??
- */
- function moveDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace ( '', '/', $aimDir );
- $aimDir = substr ( $aimDir, - 1 ) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace ( '', '/', $oldDir );
- $oldDir = substr ( $oldDir, - 1 ) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir ( $oldDir )) {
- return false;
- }
- if (!file_exists ( $aimDir )) {
- $this->createDir ( $aimDir );
- }
- @$dirHandle = opendir ( $oldDir );
- if (! $dirHandle) {
- false? ?????.
- }
- while ( false !== ($file = readdir ( $dirHandle )) ) {
- if ($file == '.' || $file == '..') {
- ?????;
- }
- if (!is_dir ( $oldDir . $file )) {
- $this->moveFile ( $oldDir . $file, $aimDir . $file, $overWrite );
- } else {
- $this->moveDir ( $oldDir . $file, $aimDir . $file, $overWrite );
- }
- }
- closeir( $dirHandle );
- rmdir( $oldDir )? ?????.
- }
- /**
- * ?? ??
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite ? ????? ?? ??? ???? ??? ?????.
- * @return ??
- */
- function moveFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists ( $fileUrl )) {
- return false;
- }
- if (file_exists ( $aimUrl ) && $overWrite = false) {
- return false;
- } elseif (file_exists ( $aimUrl ) && $overWrite = true) {
- $this->unlinkFile ( $aimUrl );
- }
- $aimDir = ???? ??( $aimUrl );
- $this->createDir( $aimDir );
- ?? ???( $fileUrl, $aimUrl );
- true? ?????.
- }
- /**
- * ?? ??
- *
- * @param string $aimDir
- * @return boolean
- */
- function unlinkDir($aimDir) {
- $aimDir = str_replace ( '', '/', $aimDir );
- $aimDir = substr ( $aimDir, - 1 ) == '/' ? $aimDir : $aimDir . '/';
- if (!is_dir ( $aimDir )) {
- return false;
- }
- $dirHandle = opendir ( $aimDir );
- while ( false !== ($file = readdir ( $dirHandle )) ) {
- if ($file == '.' || $file == '..') {
- ??;
- }
- if (!is_dir ( $aimDir . $file )) {
- $this->unlinkFile ( $aimDir . $file );
- } else {
- $this->unlinkDir ( $aimDir . $file );
- }
- }
- closeir( $dirHandle );
- rmdir( $aimDir )? ?????.
- }
- /**
- * ?? ??
- *
- * @param string $aimUrl
- * @return boolean
- */
- function unlinkFile($aimUrl) {
- if (file_exists ( $aimUrl )) {
- unlink ( $aimUrl );
- true? ?????.
- } else {
- false? ?????.
- }
- }
- /**
- * ?? ??
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite ? ????? ?? ??? ???? ??? ?????.
- * @ ?? ?? ??
- */
- function copyDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace ( '', '/ ', $aimDir );
- $aimDir = substr ( $aimDir, - 1 ) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace ( '', '/', $oldDir );
- $oldDir = substr ( $oldDir, - 1 ) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir ( $oldDir )) {
- return false;
- }
- if (!file_exists ( $aimDir )) {
- $this->createDir ( $aimDir );
- }
- $dirHandle = opendir ( $oldDir );
- while ( false !== ($file = readdir ( $dirHandle )) ) {
- if ($file == '.' || $file == '..') {
- ??;
- }
- if (!is_dir ( $oldDir . $file )) {
- $this->copyFile ( $oldDir . $file, $aimDir . $file, $overWrite );
- } else {
- $this->copyDir ( $oldDir . $file, $aimDir . $file, $overWrite );
- }
- }
- return closeir( $dirHandle );
- }
- /**
- * ?? ??
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite ? ????? ?? ??? ???? ??? ?????.
- * @return ??
- */
- function copyFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists ( $fileUrl )) {
- return ??;
- }
- if (file_exists ( $aimUrl ) && $overWrite == false) {
- return false;
- } elseif (file_exists ( $aimUrl ) && $overWrite == true) {
- $this->unlinkFile ( $aimUrl );
- }
- $aimDir = ???? ??( $aimUrl );
- $this->createDir( $aimDir );
- ??( $fileUrl, $aimUrl );
- true? ?????.
- }
- /**
- * ??? ??? ??
- *
- * @param string $filename ?? ??
- * @param boolean $str ? ?? ???
- */
- function writeFile($filename, $str) {
- if (function_exists ( file_put_contents )) {
- file_put_contents ( $filename, $str ) ;
- } else {
- $fp = fopen ( $filename, "wb" );
- fwrite ( $fp, $str );
- fclose( $fp );
- }
- }
- /**
- * ?? ?? ??? ???? ?????.
- *
- * @param string $filename filename
- * @return array
- */
- function readsFile($filename) {
- if (function_exists ( file_get_contents )) {
- return file_get_contents ( $filename );
- } else {
- $fp = fopen ( $filename, "rb" );
- $str = fread ( $fp, ?? ?? ( $filename ) );
- fclose( $fp );
- $str? ?????;
- }
- }
- /**
- * ?? ??? ??? ?????.
- *
- * @param string $filename ?? ??
- * @return ??
- */
- function readFile2array($filename) {
- $file = ??( $filename );
- $arr = ??();
- foreach( $file as $value ) {
- $arr [] = ??( $value );
- }
- $arr ??;
- }
- /**
- * /? ??
- *
- * @param string $path path
- * @return string path
- */
- function dirPath($path) {
- $path = str_replace ( '\', '/', $path );
- if (substr ( $path, - 1 ) != '/')
- $path = $path . '/';
- $path? ?????.
- }
- /**
- * ???? ??? ?? ?? ??? ??? ?????.
- *
- * @param string $in_charset ?? ?? ??
- * @param string $out_charset ?? ?? ??
- * @param string $dir ???? ??
- * @param string $fileexts ??? ?? ??
- * @return string ?? ?? ??? ?? ?? ??? ???? false? ????, ??? ??? true? ?????.
- */
- ?? dirIconv($in_charset, $out_charset, $dir, $fileexts = 'php|html|htm|shtml|shtm|js|txt|xml' ) {
- if ($in_charset == $out_charset)
- return false;
- $list = $this->dirList ( $dir );
- foreach ( $list as $v ) {
- if (preg_match ( "/.($fileexts)/i", $v ) && is_file ( $v )) {
- file_put_contents ( $v, iconv ( $in_charset, $out_charset, file_get_contents ( $v ) ) );
- }
- }
- true? ?????.
- }
- /**
- * ????? ?? ?? ?? ??
- *
- * @param string $path path
- * @param string $exts Extension
- * @param array $list ??? ?? ??
- * @return array ??? ???? ?? ??
- */
- function dirList($path, $exts = '', $list = array()) {
- $path = $this->dirPath ($??);
- $files = glob ( $path . '*' );
- foreach ( $files as $v ) {
- $fileext = $this->fileext ( $v );
- if (! $exts || preg_match ( "/.($exts)/i", $v )) {
- $list [] = $v;
- if (is_dir ( $v )) {
- $list = $this->dirList ( $v, $exts, $list );
- }
- }
- }
- return $list;
- }
- /**
- * ?? ????? ?? ?? ??? ?? ? ?? ??? ??
- *
- * @param string $path path
- * @param int $mtime ?? ??
- * @param int $atime access Time
- * @return ??? ????? ??? false? ????, ??? ??? true? ?????.
- */
- function dirTouch($path, $mtime = TIME, $atime = TIME) {
- if (!is_dir ( $path ))
- ??? ??;
- $path = $this->dirPath ( $path );
- if (!is_dir ( $path ))
- touch ( $path, $mtime, $atime );
- $files = glob ( $path . '*' );
- foreach ( $files as $v ) {
- is_dir ( $v ) ? $this->dirTouch( $v, $mtime, $atime ) : ??( $v, $mtime, $atime );
- }
- true? ?????.
- }
- /**
- * ???? ??
- *
- * @param string $dir ??
- * @param int $parentid ?? ID
- * @param array ????? ??? $dirs
- * @return ?? ???? ? ?? ???? ??? ?????
- */
- function dirTree($dir, $parentid = 0, $dirs = array()) {
- ?? $id;
- if ($parentid == 0)
- $id = 0;
- $list = glob ( $dir . '*' );
- foreach ( $list as $v ) {
- if (is_dir ( $v )) {
- $id ;
- $dirs [$id] = ??('id' => $id, 'parentid' => $parentid, 'name' => ?? ??( $v ), 'dir' => $v .'/' );
- $dirs = $this->dirTree ( $v . '/', $id, $dirs );
- }
- }
- return $dirs;
- }
- /**
- * ???? ??
- *
- * @param string $dir path
- * @return array ???? ?? ??
- */
- function dirNodeTree($dir) {
- $d = dir ( $dir );
- $dirs = ??();
- while ( false !== ($entry = $d->read ()) ) {
- if ($entry != '.' and $entry != '..' and is_dir ( $dir . '/' . $entry )) {
- $dirs[] = $entry;
- }
- }
- return $dirs;
- }
- /**
- * ???? ?? ????
- *
- * @param string $dirname ????
- * @return ??? ?? B
- */
- function getDirSize($dirname) {
- if (!file_exists ( $dirname ) ?? !is_dir ( $dirname ))
- return false;
- if (! $handle = opendir ( $dirname ))
- return false;
- $?? = 0;
- while ( false !== ($file = readdir ( $handle )) ) {
- if ($file == "." or $file == "..")
- ??;
- $file = $dirname . "/" . $??;
- if (is_dir ( $file )) {
- $size = $this->getDirSize ( $file );
- } else {
- $size = ?? ??( $file );
- }
-
- }
- closeir ( $handle );
- $size? ?????.
- }
- /**
- * ???? Kb ?? Mb? ??...
- * $size ????? ??? ?????.
- */
- function bitSize($size) {
- if (! preg_match ( "/^[0-9] $/", $num ))
- 0? ?????.
- $type = array ("B", "KB", "MB", "GB", "TB", "PB" );
-
- $j = 0;
- while ( $num >= 1024 ) {
- if ($j >= 5)
- return $num . $?? [$j];
- $num = $num / 1024;
- $j ;
- }
- $num? ?????. $?? [$j];
- }
- /**
- * ?? ?? ??? ????
- *
- * @param string $filename
- * @return string
- */
- function fileext($filename) {
- return addlashes ( Trim ( substr ( strrchr ( $filename, '.' ), 1, 10 ) ) );
- }
- }
- ?>
復(fù)代碼
|