| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 文件及文件夹处理类
- // +----------------------------------------------------------------------
- namespace util;
- use FilesystemIterator;
- use RecursiveDirectoryIterator;
- use RecursiveIteratorIterator;
- class File
- {
- /**
- * 创建目录
- * @param string $dir 目录名
- * @return bool true 成功, false 失败
- */
- public static function mk_dir(string $dir): bool
- {
- if (is_dir($dir)) {
- return true;
- }
- return mkdir($dir, 0755, true);
- }
- /**
- * 读取文件内容
- * @param string $filename 文件名
- * @return false|string
- */
- public static function read_file(string $filename): false | string
- {
- $content = '';
- if (function_exists('file_get_contents')) {
- @$content = file_get_contents($filename);
- } else {
- if (@$fp = fopen($filename, 'r')) {
- @$content = fread($fp, filesize($filename));
- @fclose($fp);
- }
- }
- return $content;
- }
- /**
- * 写文件
- * @param string $filename 文件名
- * @param string $writetext 要写入的字符串
- * @param string $openmod 打开方式
- * @return bool
- */
- public static function write_file(string $filename, string $writetext, string $openmod = 'w'): bool
- {
- if (@$fp = fopen($filename, $openmod)) {
- flock($fp, 2);
- fwrite($fp, $writetext);
- fclose($fp);
- return true;
- } else {
- return false;
- }
- }
- /**
- * 删除文件夹
- * @param string $dirname 目录
- * @param bool $withself 是否删除自身
- * @return boolean
- */
- public static function del_dir(string $dirname, bool $withself = true): bool
- {
- if (!is_dir($dirname)) {
- return false;
- }
- $files = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
- RecursiveIteratorIterator::CHILD_FIRST
- );
- foreach ($files as $fileinfo) {
- $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
- $todo($fileinfo->getRealPath());
- }
- if ($withself) {
- @rmdir($dirname);
- }
- return true;
- }
- /**
- * 复制文件夹
- * @param string $source 源文件夹
- * @param string $dest 目标文件夹
- */
- public static function copy_dir(string $source, string $dest)
- {
- if (!is_dir($dest)) {
- self::mk_dir($dest);
- }
- foreach (
- $iterator = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
- RecursiveIteratorIterator::SELF_FIRST
- ) as $item
- ) {
- if ($item->isDir()) {
- $sontDir = $dest . DS . $iterator->getSubPathName();
- if (!is_dir($sontDir)) {
- self::mk_dir($sontDir);
- }
- } else {
- copy($item, $dest . DS . $iterator->getSubPathName());
- }
- }
- }
- /**
- * 列出目录
- * @param string $dir 目录名
- * @return array 目录数组。列出文件夹下内容,返回数组 $result['dir']:存文件夹;$result['file']:存文件
- */
- public static function get_dirs(string $dir): array
- {
- $result = [
- 'dir' => [],
- 'file' => [],
- ];
- if (!is_dir($dir)) {
- return $result;
- }
- $iterator = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
- foreach ($iterator as $fileinfo) {
- $type = $fileinfo->isDir() ? 'dir' : 'file';
- $result[$type][] = $fileinfo->getFilename();
- }
- return $result;
- }
- /**
- * 取得目录下面的文件信息
- * @param string $pathname 路径
- * @param string $pattern
- * @return array
- */
- public static function listFile(string $pathname, string $pattern = '*'): array
- {
- if (str_contains($pattern, '|')) {
- $patterns = explode('|', $pattern);
- } else {
- $patterns[0] = $pattern;
- }
- $i = 0;
- $dir = [];
- foreach ($patterns as $pattern) {
- $list = glob($pathname . $pattern);
- if ($list !== false) {
- foreach ($list as $file) {
- //$dir[$i]['filename'] = basename($file);
- //basename取中文名出问题.改用此方法
- //编码转换.把中文的调整一下.
- $dir[$i]['filename'] = preg_replace('/^.+[\\\\\\/]/', '', $file);
- $dir[$i]['pathname'] = realpath($file);
- $dir[$i]['owner'] = fileowner($file);
- $dir[$i]['perms'] = fileperms($file);
- $dir[$i]['inode'] = fileinode($file);
- $dir[$i]['group'] = filegroup($file);
- $dir[$i]['path'] = dirname($file);
- $dir[$i]['atime'] = fileatime($file);
- $dir[$i]['ctime'] = filectime($file);
- $dir[$i]['size'] = filesize($file);
- $dir[$i]['type'] = filetype($file);
- $dir[$i]['ext'] = is_file($file) ? strtolower(substr(strrchr(basename($file), '.'), 1)) : '';
- $dir[$i]['mtime'] = filemtime($file);
- $dir[$i]['isDir'] = is_dir($file);
- $dir[$i]['isFile'] = is_file($file);
- $dir[$i]['isLink'] = is_link($file);
- //$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
- $dir[$i]['isReadable'] = is_readable($file);
- $dir[$i]['isWritable'] = is_writable($file);
- $i++;
- }
- }
- }
- // 对结果排序 保证目录在前面
- usort($dir, function ($a, $b) {
- if (($a["isDir"] && $b["isDir"]) || (!$a["isDir"] && !$b["isDir"])) {
- return $a["filename"] > $b["filename"] ? 1 : -1;
- } else {
- if ($a["isDir"]) {
- return -1;
- } elseif ($b["isDir"]) {
- return 1;
- }
- if ($a["filename"] == $b["filename"]) {
- return 0;
- }
- return $a["filename"] > $b["filename"] ? -1 : 1;
- }
- });
- return $dir;
- }
- /**
- * 统计文件夹大小
- * @param string $dir 目录名
- * @return int 文件夹大小(单位 B)
- */
- public static function get_size(string $dir): int
- {
- $dirlist = opendir($dir);
- $dirsize = 0;
- while (false !== ($folderorfile = readdir($dirlist))) {
- if ($folderorfile != "." && $folderorfile != "..") {
- if (is_dir("$dir/$folderorfile")) {
- $dirsize += self::get_size("$dir/$folderorfile");
- } else {
- $dirsize += filesize("$dir/$folderorfile");
- }
- }
- }
- closedir($dirlist);
- return $dirsize;
- }
- /**
- * 检测是否为空文件夹
- * @param string $dir 目录名
- * @return bool true 空, fasle 不为空
- */
- public static function empty_dir(string $dir): bool
- {
- return (($files = @scandir($dir)) && count($files) <= 2);
- }
- /**
- * 移除空目录
- * @param string $dir 目录
- * @return void
- */
- public static function remove_empty_folder(string $dir): void
- {
- try {
- $isDirEmpty = !(new \FilesystemIterator($dir))->valid();
- if ($isDirEmpty) {
- @rmdir($dir);
- self::remove_empty_folder(dirname($dir));
- }
- } catch (\UnexpectedValueException $e) {
- } catch (\Exception $e) {
- }
- }
- /**
- * 判断文件或文件夹是否可写.
- * @param string $file 文件或目录
- * @return bool
- */
- public static function is_really_writable(string $file): bool
- {
- if (DIRECTORY_SEPARATOR === '/') {
- return is_writable($file);
- }
- if (is_dir($file)) {
- $file = rtrim($file, '/') . '/' . md5(mt_rand());
- if (($fp = @fopen($file, 'ab')) === false) {
- return false;
- }
- fclose($fp);
- @chmod($file, 0777);
- @unlink($file);
- return true;
- } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
- return false;
- }
- fclose($fp);
- return true;
- }
- }
|