| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace common\helpers;
- use yii\helpers\BaseFileHelper;
- /**
- * Class FileHelper
- * @package common\helpers
- */
- class FileHelper extends BaseFileHelper
- {
- /**
- * 检测目录并循环创建目录
- *
- * @param $catalogue
- * @return bool
- */
- public static function mkdirs($catalogue)
- {
- if (!file_exists($catalogue)) {
- self::mkdirs(dirname($catalogue));
- mkdir($catalogue, 0777);
- }
- return true;
- }
- /**
- * 写入日志
- *
- * @param $path
- * @param $content
- * @return bool|int
- */
- public static function writeLog($path, $content)
- {
- self::mkdirs(dirname($path));
- $content = '[' . date('Y-m-d H:i:s') . '] ' . $content;
- return file_put_contents($path, "\r\n" . $content, FILE_APPEND);
- }
- /**
- * 获取文件夹大小
- *
- * @param string $dir 根文件夹路径
- * @return int
- */
- public static function getDirSize($dir)
- {
- $handle = opendir($dir);
- $sizeResult = 0;
- while (false !== ($folderOrFile = readdir($handle))) {
- if ($folderOrFile != "." && $folderOrFile != "..") {
- if (is_dir("$dir/$folderOrFile")) {
- $sizeResult += self::getDirSize("$dir/$folderOrFile");
- } else {
- $sizeResult += filesize("$dir/$folderOrFile");
- }
- }
- }
- closedir($handle);
- return $sizeResult;
- }
- /**
- * 基于数组创建目录
- *
- * @param $files
- */
- public static function createDirOrFiles($files)
- {
- foreach ($files as $key => $value) {
- if (substr($value, -1) == '/') {
- mkdir($value);
- } else {
- file_put_contents($value, '');
- }
- }
- }
- /**
- * 软著文件生成
- *
- * @param $dir
- * @param $savePath
- */
- public static function getDirFileContent($dir, $savePath, $suffix = ['php'])
- {
- $handle = opendir($dir);
- while (false !== ($folderOrFile = readdir($handle))) {
- if ($folderOrFile != "." && $folderOrFile != "..") {
- if (is_dir("$dir/$folderOrFile")) {
- self::getDirFileContent("$dir/$folderOrFile", $savePath, $suffix);
- } else {
- $array = explode('.', $folderOrFile);
- if (in_array(end($array), $suffix)) {
- // 去除注释
- $str = StringHelper::removeAnnotation(file_get_contents("$dir/$folderOrFile"));
- // 追加写入
- file_put_contents($savePath, $str, FILE_APPEND);
- }
- }
- }
- }
- }
- /**
- * @name:
- * @msg: 把一个或者多个文件打包压缩为一个文件
- * @param {array} $files 需要打包压缩的文件
- * @param {string} $zip_filename 生成的压缩包文件名称
- * @return string 压缩成功返回生成的压缩文件的路径,否则返回 false
- */
- public static function packFiles(array $files = [], string $zip_filename = '')
- {
- try {
- if (empty($files)) return false;
- $path = \Yii::getAlias('@attachment') . '/download/zips';
- if (!file_exists($path)) {
- mkdir($path, 0755, true);
- }
- $temp_path = \Yii::getAlias('@attachment') . '/download/temp';
- if (!file_exists($temp_path)) {
- mkdir($temp_path, 0755, true);
- }
- $zip = new \ZipArchive();
- if (empty($zip_filename)) {
- $zip_filename = $path . '/' . '压缩文件-' . date('YmdHis') . mt_rand(0, 100) . '.zip';
- } else {
- $zip_filename = $path . '/' . $zip_filename . '-' . date('YmdHis') . mt_rand(0, 100) . '.zip';
- }
- $zip->open($zip_filename, \ZipArchive::CREATE);
- foreach ($files as $file) {
- $csv_filename = $temp_path . '/' . pathinfo($file, PATHINFO_BASENAME);
- if (!file_exists($csv_filename)) {
- $csv_str = file_get_contents($file);
- file_put_contents($csv_filename, $csv_str);
- }
- $zip->addFile($csv_filename, basename($csv_filename));
- }
- $zip->close();
- return $zip_filename;
- } catch (\Exception $e) {
- \Yii::error('打包压缩文件失败,errmsg:' . $e->getMessage());
- \Yii::getLogger()->flush(true);
- return false;
- }
- }
- public static function downFile($save_path,$file_name,$file_url){
- $path_info_arr = pathinfo($file_url);
- if(!is_dir($save_path)) mkdir($save_path,0777,true);
- $file = $save_path.'/'.$file_name.'.'.$path_info_arr['extension'];
- $file_resource = file_get_contents($file_url);
- $res = file_put_contents($file,$file_resource);
- if($res) return $file;
- return '';
- }
- public static function recursiveDelete($directory){
- if (!$handler = opendir($directory)) {
- return false;
- }
- while (($file = readdir($handler)) !== false) {
- if ($file !== '.' && $file !== '..') {
- $file = $directory . '/' . $file;
- if (is_dir($file)) {
- self::recursiveDelete($file);
- } else {
- @unlink($file);
- }
- }
- }
- @rmdir($directory);
- }
- }
|