-
- /**
- * File directory operation class
- * Editor: bbs.it-home.org
- * Example:
- * $fileutil = new fileDirUtil();
- * $fileutil->createDir('a/1/2/3') ; To test the creation folder, create a/1/2/3 folder
- * $fileutil->createFile('b/1/2/3'); To test the creation file, create it under the b/1/2/ folder A 3-file
- * $fileutil->createFile('b/1/2/3.txt'); To test the creation file, create a 3.exe file under the b/1/2/ folder
- * $fileutil-> ;writeFile('b/1/2/3.txt','this is something i write!'); Write content in the file
- * $arr = $fileutil->readFile2array('example/mysql.txt') ;
- * $arr = $fileutil->readsFile('example/mysql.txt');
- * $size=$fileutil->bitSize($fileutil->getDirSize("example")); Get the file or The size of the directory
- * $fileutil->copyDir('b','d/e'); Test the copy folder to create a d/e folder and copy the contents of the b folder into it
- * $fileutil-> ;copyFile('b/1/2/3.exe','b/b/3.exe'); Test copy the file to create a b/b folder, and put 3. in the b/1/2 folder. Copy the exe file into it
- * $fileutil->moveDir('a/','b/c'); Test the moving folder, create a b/c folder, move the contents of the a folder into it, and delete it a folder
- * $fileutil->moveFile('b/1/2/3.exe','b/d/3.exe'); Test moving files to create a b/d folder and put b/ Move the 3.exe in 1/2 in
- * $fileutil->unlinkFile('b/d/3.exe'); Test deletion of the file and delete the b/d/3.exe file
- * $fileutil->unlinkDir ('d'); Test deletion folder to delete d folder
- * $list = $fileutil->dirList("E:example"); Test list folder to list all files in the directory
- * $list = $fileutil ->dirTree("/"); Test list folder tree lists the direct tree relationship of all files in the directory
- */
- class fileDirUtil {
- /**
- * Create folder
- *
- * @param string $aimUrl
- * @return viod
- */
- function createDir($aimUrl, $mode = 0777) {
- $aimUrl = str_replace ( '', '/', $aimUrl );
- $aimDir = '';
- $arr = explode ( '/', $aimUrl );
- foreach ( $arr as $str ) {
- $aimDir .= $str . '/';
- if (! file_exists ( $aimDir )) {
- mkdir ( $aimDir, $mode );
- }
- }
- }
- /**
- * Create file
- *
- * @param string $aimUrl
- * @param boolean $overWrite This parameter controls whether to overwrite the original file
- * @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 = dirname ( $aimUrl );
- $this->createDir ( $aimDir );
- touch ( $aimUrl );
- return true;
- }
- /**
- * Move folder
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite This parameter controls whether to overwrite the original file
- * @return boolean
- */
- 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) {
- return false;
- }
- while ( false !== ($file = readdir ( $dirHandle )) ) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (! is_dir ( $oldDir . $file )) {
- $this->moveFile ( $oldDir . $file, $aimDir . $file, $overWrite );
- } else {
- $this->moveDir ( $oldDir . $file, $aimDir . $file, $overWrite );
- }
- }
- closedir ( $dirHandle );
- return rmdir ( $oldDir );
- }
- /**
- * Move file
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite This parameter controls whether to overwrite the original file
- * @return boolean
- */
- 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 = dirname ( $aimUrl );
- $this->createDir ( $aimDir );
- rename ( $fileUrl, $aimUrl );
- return true;
- }
- /**
- * Delete folder
- *
- * @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 == '..') {
- continue;
- }
- if (! is_dir ( $aimDir . $file )) {
- $this->unlinkFile ( $aimDir . $file );
- } else {
- $this->unlinkDir ( $aimDir . $file );
- }
- }
- closedir ( $dirHandle );
- return rmdir ( $aimDir );
- }
- /**
- * Delete file
- *
- * @param string $aimUrl
- * @return boolean
- */
- function unlinkFile($aimUrl) {
- if (file_exists ( $aimUrl )) {
- unlink ( $aimUrl );
- return true;
- } else {
- return false;
- }
- }
- /**
- * Copy folder
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite This parameter controls whether to overwrite the original file
- * @return boolean
- */
- 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 == '..') {
- continue;
- }
- if (! is_dir ( $oldDir . $file )) {
- $this->copyFile ( $oldDir . $file, $aimDir . $file, $overWrite );
- } else {
- $this->copyDir ( $oldDir . $file, $aimDir . $file, $overWrite );
- }
- }
- return closedir ( $dirHandle );
- }
- /**
- * Copy file
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite This parameter controls whether to overwrite the original file
- * @return boolean
- */
- function copyFile($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 = dirname ( $aimUrl );
- $this->createDir ( $aimDir );
- copy ( $fileUrl, $aimUrl );
- return true;
- }
- /**
- * Write string to file
- *
- * @param string $filename file name
- * @param boolean $str character data to be written
- */
- 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 );
- }
- }
- /**
- * Read the entire file content into a string
- *
- * @param string $filename file name
- * @return array
- */
- function readsFile($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;
- }
- }
- /**
- * Read the file content into an array
- *
- * @param string $filename file name
- * @return array
- */
- function readFile2array($filename) {
- $file = file ( $filename );
- $arr = array ();
- foreach ( $file as $value ) {
- $arr [] = trim ( $value );
- }
- return $arr;
- }
- /**
- * Convert to /
- *
- * @param string $path path
- * @return string path
- */
- function dirPath($path) {
- $path = str_replace ( '\', '/', $path );
- if (substr ( $path, - 1 ) != '/')
- $path = $path . '/';
- return $path;
- }
- /**
- * Convert all file encoding formats under the directory
- *
- * @param string $in_charset original character set
- * @param string $out_charset target character set
- * @param string $dir directory address
- * @param string $fileexts converted File format
- * @return string If the original character set and the target character set are the same, return false, otherwise true
- */
- function 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 ) ) );
- }
- }
- return true;
- }
- /**
- * List all files in the directory
- *
- * @param string $path path
- * @param string $exts extension
- * @param array $list added file list
- * @return array all files that meet the conditions
- */
- function dirList($path, $exts = '', $list = array()) {
- $path = $this->dirPath ( $path );
- $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;
- }
- /**
- * Set the access and modification time of all files under the directory
- *
- * @param string $path path
- * @param int $mtime modification time
- * @param int $atime access time
- * @return array Return when it is not a directory false, otherwise return true
- */
- function dirTouch($path, $mtime = TIME, $atime = TIME) {
- if (! is_dir ( $path ))
- return false;
- $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 ) : touch ( $v, $mtime, $atime );
- }
- return true;
- }
- /**
- * Directory list
- *
- * @param string $dir path
- * @param int $parentid parent id
- * @param array $dirs passed in directory
- * @return array returns directory and subdirectory list
- */
- function dirTree($dir, $parentid = 0, $dirs = array()) {
- global $id;
- if ($parentid == 0)
- $id = 0;
- $list = glob ( $dir . '*' );
- foreach ( $list as $v ) {
- if (is_dir ( $v )) {
- $id ++;
- $dirs [$id] = array ('id' => $id, 'parentid' => $parentid, 'name' => basename ( $v ), 'dir' => $v . '/' );
- $dirs = $this->dirTree ( $v . '/', $id, $dirs );
- }
- }
- return $dirs;
- }
- /**
- * Directory list
- *
- * @param string $dir path
- * @return array Return directory list
- */
- function dirNodeTree($dir) {
- $d = dir ( $dir );
- $dirs = array();
- while ( false !== ($entry = $d->read ()) ) {
- if ($entry != '.' and $entry != '..' and is_dir ( $dir . '/' . $entry )) {
- $dirs[] = $entry;
- }
- }
- return $dirs;
- }
- /**
- * Get the directory size
- *
- * @param string $dirname directory
- * @return string bit B
- */
- function getDirSize($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 . "/" . $file;
- if (is_dir ( $file )) {
- $size += $this->getDirSize ( $file );
- } else {
- $size += filesize ( $file );
- }
-
- }
- closedir ( $handle );
- return $size;
- }
- /**
- * Convert bytes to Kb or Mb...
- * Parameter $size is the byte size
- */
- function bitSize($size) {
- if (! preg_match ( "/^[0-9]+$/", $num ))
- return 0;
- $type = array ("B", "KB", "MB", "GB", "TB", "PB" );
-
- $j = 0;
- while ( $num >= 1024 ) {
- if ($j >= 5)
- return $num . $type [$j];
- $num = $num / 1024;
- $j ++;
- }
- return $num . $type [$j];
- }
- /**
- * Get the file name suffix
- *
- * @param string $filename
- * @return string
- */
- function fileext($filename) {
- return addslashes ( trim ( substr ( strrchr ( $filename, '.' ), 1, 10 ) ) );
- }
- }
- ?>
复制代码
|