| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- /**
- * The zfile library of zentaopms.
- *
- * @copyright Copyright 2009-2015 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
- * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
- * @author Chunsheng Wang <chunsheng@cnezsoft.com>
- * @package Zfile
- * @version $Id: zfile.class.php 2605 2013-01-09 07:22:58Z wwccss $
- * @link http://www.zentao.net
- */
- class zfile
- {
- /**
- * Copy a directory from an directory to another directory.
- *
- * @param string $from
- * @param string $to
- * @param bool $logLevel
- * @param string $logFile
- * @param array $excludeFiles
- * @param bool $toIsLink
- * @access public
- * @return array copied files, count, size or message.
- */
- public function copyDir($from, $to, $logLevel = false, $logFile = '', $excludeFiles = array(), $toIsLink = false)
- {
- if(is_file($from)) return false;
- static $copiedFiles = array();
- static $errorFiles = array();
- $count = $size = 0;
- $log['copiedFiles'] = array();
- $log['count'] = 0;
- $log['size'] = 0;
- if(!is_dir($from) or !is_readable($from))
- {
- if(!is_dir($from)) $log['message'] = "$from: Dir is not exists";
- if(!is_readable($from)) $log['message'] = "$from: Permission denied";
- }
- if(!is_dir($to) and !@mkdir($to, 0777, true)) $log['message'] = "$to: Permission denied";
- if($logFile and !file_exists($logFile)) touch($logFile);
- if(!empty($log['message'])) return $log;
- $from = realpath($from) . '/';
- if(is_link($to) || $toIsLink)
- {
- $to = $to . '/';
- $toIsLink = true;
- }
- else
- {
- $to = realpath($to) . '/';
- }
- $entries = scandir($from);
- foreach($entries as $entry)
- {
- if($entry == '.' or $entry == '..' or $entry == '.svn' or $entry == '.git' or in_array($entry, $excludeFiles)) continue;
- $fullEntry = $from . $entry;
- if(is_file($fullEntry))
- {
- if(is_dir($to) and !is_writeable($to)) $log['message'] = "$to: Permission denied";
- if(!empty($log['message'])) return $log;
- if(file_exists($to . $entry)) unlink($to . $entry);
- if(copy($fullEntry, $to . $entry))
- {
- if($logLevel) $copiedFiles[] = $to . $entry;
- $count += 1;
- $size += filesize($fullEntry);
- if($logFile and file_exists($logFile))
- {
- $summary = json_decode(file_get_contents($logFile), true);
- if(empty($summary)) $summary = array();
- if(empty($summary['count'])) $summary['count'] = 0;
- $summary['count'] += 1;
- file_put_contents($logFile, json_encode($summary));
- }
- }
- else
- {
- $errorFiles[] = $fullEntry;
- }
- }
- else
- {
- $nextFrom = $fullEntry;
- $nextTo = $to . $entry;
- $result = $this->copyDir($nextFrom, $nextTo, $logLevel, $logFile, array(), $toIsLink);
- $count += $result['count'];
- $size += $result['size'];
- }
- }
- if($logLevel) $log['copiedFiles'] = $copiedFiles;
- $log['errorFiles'] = $errorFiles;
- $log['count'] = $count;
- $log['size'] = $size;
- $log['logFile'] = $logFile;
- return $log;
- }
- /**
- * Get count.
- *
- * @param string $dir
- * @param array $excludeFiles
- * @access public
- * @return int
- */
- public function getCount($dir, $excludeFiles = array())
- {
- if(!file_exists($dir)) return 0;
- if(is_file($dir)) return 1;
- $count = 0;
- $entries = scandir($dir);
- foreach($entries as $entry)
- {
- if($entry == '.' or $entry == '..' or $entry == '.svn' or $entry == '.git' or in_array($entry, $excludeFiles)) continue;
- $fullEntry = $dir . '/' . $entry;
- $count += $this->getCount($fullEntry);
- }
- return $count;
- }
- /**
- * Remove a dir.
- *
- * @param string $dir
- * @access public
- * @return bool
- */
- public function removeDir($dir)
- {
- if(empty($dir)) return true;
- $dir = realpath($dir) . '/';
- if($dir == '/') return false;
- if(!is_writable($dir)) return false;
- if(!is_dir($dir)) return true;
- $entries = scandir($dir);
- foreach($entries as $entry)
- {
- if($entry == '.' or $entry == '..' or $entry == '.svn') continue;
- $fullEntry = $dir . $entry;
- if(is_file($fullEntry))
- {
- unlink($fullEntry);
- }
- else
- {
- $this->removeDir($fullEntry);
- }
- }
- if(!@rmdir($dir)) return false;
- return true;
- }
- /**
- * Get files under a directory recursive.
- *
- * @param string $dir
- * @param array $exceptions
- * @access private
- * @return array
- */
- public function readDir($dir, $exceptions = array())
- {
- static $files = array();
- if(!is_dir($dir)) return $files;
- $dir = realpath($dir) . '/';
- $entries = scandir($dir);
- foreach($entries as $entry)
- {
- if($entry == '.' or $entry == '..' or $entry == '.svn') continue;
- if(in_array($entry, $exceptions)) continue;
- $fullEntry = $dir . $entry;
- if(is_file($fullEntry))
- {
- $files[] = $dir . $entry;
- }
- else
- {
- $nextDir = $dir . $entry;
- $this->readDir($nextDir);
- }
- }
- return $files;
- }
- /**
- * Make a dir.
- *
- * @param string $dir
- * @access public
- * @return bool
- */
- public function mkdir($dir)
- {
- return mkdir($dir, 0755, true);
- }
- /**
- * Remove a file
- *
- * @param string $file
- * @access public
- * @return bool
- */
- public function removeFile($file)
- {
- if(!file_exists($file)) return true;
- return @unlink($file);
- }
- /**
- * Batch remove files. use glob function.
- *
- * @param string $patern
- * @access public
- * @return void
- */
- public function batchRemoveFile($patern)
- {
- $files = glob($patern);
- foreach($files as $file) @unlink($file);
- }
- /**
- * Remove a file
- *
- * @param string from
- * @param string to
- * @access public
- * @return bool
- */
- public function copyFile($from, $to)
- {
- return @copy($from, $to);
- }
- /**
- * Rename a file or directory.
- *
- * @param string from
- * @param string to
- * @access public
- * @return bool
- */
- public function rename($from, $to)
- {
- return rename($from, $to);
- }
- /**
- * Get file size.
- *
- * @param string $file
- * @access public
- * @return int
- */
- public function getFileSize($file)
- {
- return abs(filesize($file));
- }
- /**
- * Get directory size.
- *
- * @param string $dir
- * @access public
- * @return int
- */
- public function getDirSize($dir)
- {
- $size = 0;
- foreach(glob("$dir/*") as $file)
- {
- if(is_dir($file))
- {
- $size += $this->getDirSize($file);
- }
- else
- {
- $size += $this->getFileSize($file);
- }
- }
- return $size;
- }
- }
|