1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084 |
- <?php
- namespace crmeb\services;
- use app\services\other\ExpressServices;
- use think\exception\ValidateException;
- class FileService
- {
-
- public static function mkDir(string $dir)
- {
- $dir = rtrim($dir, '/') . '/';
- if (!is_dir($dir)) {
- if (mkdir($dir, 0700) == false) {
- return false;
- }
- return true;
- }
- return true;
- }
-
- public static function writeFile(string $filename, string $writetext, string $openmod = 'w')
- {
- if (@$fp = fopen($filename, $openmod)) {
- flock($fp, 2);
- fwrite($fp, $writetext);
- fclose($fp);
- return true;
- } else {
- return false;
- }
- }
-
- public static function del_where_dir($path, $start = '', $end = '')
- {
- if (!file_exists($path)) {
- return false;
- }
- $dh = @opendir($path);
- if ($dh) {
- while (($d = readdir($dh)) !== false) {
- if ($d == '.' || $d == '..') {
- continue;
- }
- $tmp = $path . '/' . $d;
- if (!is_dir($tmp)) {
- $file_time = filemtime($tmp);
- if ($file_time) {
- if ($start != '' && $end != '') {
- if ($file_time >= $start && $file_time <= $end) {
- @unlink($tmp);
- }
- } elseif ($start != '' && $end == '') {
- if ($file_time >= $start) {
- @unlink($tmp);
- }
- } elseif ($start == '' && $end != '') {
- if ($file_time <= $end) {
- @unlink($tmp);
- }
- } else {
- @unlink($tmp);
- }
- }
- } else {
- self::delDir($tmp, $start, $end);
- }
- }
-
- $count = count(scandir($path));
- closedir($dh);
- if ($count <= 2) @rmdir($path);
- }
- return true;
- }
-
- public static function delDir($dirName)
- {
- if (!file_exists($dirName)) {
- return false;
- }
- $dir = opendir($dirName);
- while ($fileName = readdir($dir)) {
- $file = $dirName . '/' . $fileName;
- if ($fileName != '.' && $fileName != '..') {
- if (is_dir($file)) {
- self::delDir($file);
- } else {
- unlink($file);
- }
- }
- }
- closedir($dir);
- return rmdir($dirName);
- }
-
- public function copyDir(string $surDir, string $toDir)
- {
- $surDir = rtrim($surDir, '/') . '/';
- $toDir = rtrim($toDir, '/') . '/';
- if (!file_exists($surDir)) {
- return false;
- }
- if (!file_exists($toDir)) {
- $this->createDir($toDir);
- }
- $file = opendir($surDir);
- while ($fileName = readdir($file)) {
- $file1 = $surDir . '/' . $fileName;
- $file2 = $toDir . '/' . $fileName;
- if ($fileName != '.' && $fileName != '..') {
- if (is_dir($file1)) {
- $this->copyDir($file1, $file2);
- } else {
- copy($file1, $file2);
- }
- }
- }
- closedir($file);
- return true;
- }
-
- static function getDirs($dir)
- {
- $dir = rtrim($dir, '/') . '/';
- $dirArray [][] = NULL;
- if (false != ($handle = opendir($dir))) {
- $i = 0;
- $j = 0;
- while (false !== ($file = readdir($handle))) {
- if (is_dir($dir . $file)) {
- $dirArray ['dir'] [$i] = $file;
- $i++;
- } else {
- $dirArray ['file'] [$j] = $file;
- $j++;
- }
- }
- closedir($handle);
- }
- return $dirArray;
- }
-
- public static function getSize($dir)
- {
- $dirlist = opendir($dir);
- $dirsize = 0;
- while (false !== ($folderorfile = readdir($dirlist))) {
- if ($folderorfile != "." && $folderorfile != "..") {
- if (is_dir("$dir/$folderorfile")) {
- $dirsize += self::getSize("$dir/$folderorfile");
- } else {
- $dirsize += filesize("$dir/$folderorfile");
- }
- }
- }
- closedir($dirlist);
- return $dirsize;
- }
-
- static function emptyDir($dir)
- {
- return (($files = @scandir($dir)) && count($files) <= 2);
- }
-
- public function createDir(string $dir, int $mode = 0777)
- {
- return is_dir($dir) or ($this->createDir(dirname($dir)) and mkdir($dir, $mode));
- }
-
- public function createFile(string $path, bool $over_write = FALSE, int $time = NULL, int $atime = NULL)
- {
- $path = $this->dirReplace($path);
- $time = empty($time) ? time() : $time;
- $atime = empty($atime) ? time() : $atime;
- if (file_exists($path) && $over_write) {
- $this->unlinkFile($path);
- }
- $aimDir = dirname($path);
- $this->createDir($aimDir);
- return touch($path, $time, $atime);
- }
-
- public function close(string $path)
- {
- return fclose($path);
- }
-
- public static function readFile(string $file)
- {
- return @file_get_contents($file);
- }
-
- public function allowUploadSize()
- {
- $val = trim(ini_get('upload_max_filesize'));
- return $val;
- }
-
- public static function byteFormat($size, $dec = 2)
- {
- $a = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
- $pos = 0;
- while ($size >= 1024) {
- $size /= 1024;
- $pos++;
- }
- return round($size, $dec) . " " . $a[$pos];
- }
-
- public function removeDir(str $dir_path, bool $is_all = FALSE)
- {
- $dirName = $this->dirReplace($dir_path);
- $handle = @opendir($dirName);
- while (($file = @readdir($handle)) !== FALSE) {
- if ($file != '.' && $file != '..') {
- $dir = $dirName . '/' . $file;
- if ($is_all) {
- is_dir($dir) ? $this->removeDir($dir) : $this->unlinkFile($dir);
- } else {
- if (is_file($dir)) {
- $this->unlinkFile($dir);
- }
- }
- }
- }
- closedir($handle);
- return @rmdir($dirName);
- }
-
- public function getBasename(string $file_path)
- {
- $file_path = $this->dirReplace($file_path);
- return basename(str_replace('\\', '/', $file_path));
-
- }
-
- public static function getExt(string $file)
- {
- $file = self::dirReplace($file);
- return pathinfo($file, PATHINFO_EXTENSION);
- }
-
- public function fatherDir(string $path, $num = 1)
- {
- $path = $this->dirReplace($path);
- $arr = explode('/', $path);
- if ($num == 0 || count($arr) < $num) return pathinfo($path, PATHINFO_BASENAME);
- return substr(strrev($path), 0, 1) == '/' ? $arr[(count($arr) - (1 + $num))] : $arr[(count($arr) - $num)];
- }
-
- public function unlinkFile(string $path)
- {
- $path = $this->dirReplace($path);
- if (file_exists($path)) {
- return unlink($path);
- }
- }
-
- public function handleFile(string $old_path, string $new_path, string $type = 'copy', bool $overWrite = FALSE)
- {
- $old_path = $this->dirReplace($old_path);
- $new_path = $this->dirReplace($new_path);
- if (file_exists($new_path) && $overWrite = FALSE) {
- return FALSE;
- } else if (file_exists($new_path) && $overWrite = TRUE) {
- $this->unlinkFile($new_path);
- }
- $aimDir = dirname($new_path);
- $this->createDir($aimDir);
- switch ($type) {
- case 'copy':
- return copy($old_path, $new_path);
- break;
- case 'move':
- return @rename($old_path, $new_path);
- break;
- }
- }
-
- public function handleDir(string $old_path, string $new_path, string $type = 'copy', bool $overWrite = FALSE)
- {
- $new_path = $this->checkPath($new_path);
- $old_path = $this->checkPath($old_path);
- if (!is_dir($old_path)) return FALSE;
- if (!file_exists($new_path)) $this->createDir($new_path);
- $dirHandle = opendir($old_path);
- if (!$dirHandle) return FALSE;
- $boolean = TRUE;
- while (FALSE !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') continue;
- if (!is_dir($old_path . $file)) {
- $boolean = $this->handleFile($old_path . $file, $new_path . $file, $type, $overWrite);
- } else {
- $this->handleDir($old_path . $file, $new_path . $file, $type, $overWrite);
- }
- }
- switch ($type) {
- case 'copy':
- closedir($dirHandle);
- return $boolean;
- break;
- case 'move':
- closedir($dirHandle);
- return @rmdir($old_path);
- break;
- }
- }
-
- public static function dirReplace(string $path)
- {
- return str_replace('//', '/', str_replace('\\', '/', $path));
- }
-
- public static function getTempltes(string $path)
- {
- $path = self::dirReplace($path);
- if (file_exists($path)) {
- $fp = fopen($path, 'r');
- $rstr = fread($fp, filesize($path));
- fclose($fp);
- return $rstr;
- } else {
- return '';
- }
- }
-
- public function rename(string $oldname, string $newname)
- {
- if (($newname != $oldname) && is_writable($oldname)) {
- return rename($oldname, $newname);
- }
- }
-
- public function getDirInfo(string $dir)
- {
- $handle = @opendir($dir);
- $directory_count = 0;
- while (FALSE !== ($file_path = readdir($handle))) {
- if ($file_path != "." && $file_path != "..") {
-
- $next_path = $dir . '/' . $file_path;
- if (is_dir($next_path)) {
- $directory_count++;
- $result_value = self::getDirInfo($next_path);
- $total_size += $result_value['size'];
- $file_cout += $result_value['filecount'];
- $directory_count += $result_value['dircount'];
- } elseif (is_file($next_path)) {
- $total_size += filesize($next_path);
- $file_cout++;
- }
- }
- }
- closedir($handle);
- $result_value['size'] = $total_size;
- $result_value['filecount'] = $file_cout;
- $result_value['dircount'] = $directory_count;
- return $result_value;
- }
-
- public function changeFileCode(string $path, string $input_code, string $out_code)
- {
- if (is_file($path))
- {
- $content = file_get_contents($path);
- $content = string::chang_code($content, $input_code, $out_code);
- $fp = fopen($path, 'w');
- $res = (bool)fputs($fp, $content);
- fclose($fp);
- return $res;
- }
- }
-
- public function changeDirFilesCode(string $dirname, string $input_code, string $out_code, bool $is_all = TRUE, string $exts = '')
- {
- if (is_dir($dirname)) {
- $fh = opendir($dirname);
- while (($file = readdir($fh)) !== FALSE) {
- if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) {
- continue;
- }
- $filepath = $dirname . '/' . $file;
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = $this->changeDirFilesCode($filepath, $input_code, $out_code, $is_all, $exts);
- } else {
- if ($this->getExt($filepath) == $exts && is_file($filepath)) {
- $boole = $this->changeFileCode($filepath, $input_code, $out_code, $is_all, $exts);
- if (!$boole) continue;
- }
- }
- }
- closedir($fh);
- return TRUE;
- } else {
- return FALSE;
- }
- }
-
- public function listDirInfo(string $dirname, bool $is_all = FALSE, string $exts = '', string $sort = 'ASC')
- {
-
- $new = strrev($dirname);
- if (strpos($new, '/') == 0) {
- $new = substr($new, 1);
- }
- $dirname = strrev($new);
- $sort = strtolower($sort);
- $files = [];
- $subfiles = [];
- if (is_dir($dirname)) {
- $fh = opendir($dirname);
- while (($file = readdir($fh)) !== FALSE) {
- if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) continue;
- $filepath = $dirname . '/' . $file;
- switch ($exts) {
- case '*':
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::listDirInfo($filepath, $is_all, $exts, $sort));
- }
- array_push($files, $filepath);
- break;
- case 'folder':
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::listDirInfo($filepath, $is_all, $exts, $sort));
- array_push($files, $filepath);
- } elseif (is_dir($filepath)) {
- array_push($files, $filepath);
- }
- break;
- case 'file':
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::listDirInfo($filepath, $is_all, $exts, $sort));
- } elseif (is_file($filepath)) {
- array_push($files, $filepath);
- }
- break;
- default:
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::listDirInfo($filepath, $is_all, $exts, $sort));
- } elseif (preg_match("/\.($exts)/i", $filepath) && is_file($filepath)) {
- array_push($files, $filepath);
- }
- break;
- }
- switch ($sort) {
- case 'asc':
- sort($files);
- break;
- case 'desc':
- rsort($files);
- break;
- case 'nat':
- natcasesort($files);
- break;
- }
- }
- closedir($fh);
- return $files;
- } else {
- return FALSE;
- }
- }
-
- public function dirInfo(string $dir)
- {
- return scandir($dir);
- }
-
- public function isEmpty(string $dir)
- {
- $handle = opendir($dir);
- while (($file = readdir($handle)) !== false) {
- if ($file != '.' && $file != '..') {
- closedir($handle);
- return true;
- }
- }
- closedir($handle);
- return false;
- }
-
- public static function listInfo(string $file)
- {
- $dir = [];
- $dir['filename'] = basename($file);
- $dir['pathname'] = strstr(php_uname('s'), 'Windows') ? str_replace('\\', '\\\\', realpath($file)) : realpath($file);
- $dir['owner'] = fileowner($file);
- $dir['perms'] = fileperms($file);
- $dir['inode'] = fileinode($file);
- $dir['group'] = filegroup($file);
- $dir['path'] = dirname($file);
- $dir['atime'] = fileatime($file);
- $dir['ctime'] = filectime($file);
- $dir['perms'] = fileperms($file);
- $dir['size'] = self::byteFormat(filesize($file), 2);
- $dir['type'] = filetype($file);
- $dir['ext'] = is_file($file) ? pathinfo($file, PATHINFO_EXTENSION) : '';
- $dir['mtime'] = filemtime($file);
- $dir['isDir'] = is_dir($file);
- $dir['isFile'] = is_file($file);
- $dir['isLink'] = is_link($file);
- $dir['isReadable'] = is_readable($file);
- $dir['isWritable'] = is_writable($file);
- $dir['isUpload'] = is_uploaded_file($file);
- return $dir;
- }
-
- public function openInfo(string $file)
- {
- $file = fopen($file, "r");
- $result = fstat($file);
- fclose($file);
- return $result;
- }
-
- public function change_file($file, $type, $ch_info)
- {
- switch ($type) {
- case 'group' :
- $is_ok = chgrp($file, $ch_info);
- break;
- case 'mode' :
- $is_ok = chmod($file, $ch_info);
- break;
- case 'ower' :
- $is_ok = chown($file, $ch_info);
- break;
- }
- }
-
- public function getFileType(string $path)
- {
-
-
-
- return pathinfo($path);
- }
-
- public function getUploadFileInfo($file)
- {
- $file_info = request()->file($file);
- $info = [];
- $info['type'] = strtolower(trim(stripslashes(preg_replace("/^(.+?);.*$/", "\\1", $file_info['type'])), '"'));
- $info['temp'] = $file_info['tmp_name'];
- $info['size'] = $file_info['size'];
- $info['error'] = $file_info['error'];
- $info['name'] = $file_info['name'];
- $info['ext'] = $this->getExt($file_info['name']);
- return $info;
- }
-
- public function setFileName(string $type)
- {
- switch ($type) {
- case 'hash' :
- mt_srand();
- $new_file = md5(uniqid(mt_rand()));
- break;
- case 'time' :
- $new_file = time();
- break;
- default :
- $new_file = date($type, time());
- break;
- }
- return $new_file;
- }
-
- public function checkPath($path)
- {
- return (preg_match('/\/$/', $path)) ? $path : $path . '/';
- }
-
- public static function downRemoteFile(string $url, string $save_dir = '', string $filename = '', int $type = 0)
- {
- if (trim($url) == '') {
- return ['file_name' => '', 'save_path' => '', 'error' => 1];
- }
- if (trim($save_dir) == '') {
- $save_dir = './';
- }
- if (trim($filename) == '') {
- $ext = strrchr($url, '.');
-
-
-
- $filename = time() . $ext;
- }
- if (0 !== strrpos($save_dir, '/')) {
- $save_dir .= '/';
- }
-
- if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
- return ['file_name' => '', 'save_path' => '', 'error' => 5];
- }
-
- if ($type) {
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $img = curl_exec($ch);
- curl_close($ch);
- } else {
- ob_start();
- readfile($url);
- $img = ob_get_contents();
- ob_end_clean();
- }
-
-
- $fp2 = fopen($save_dir . $filename, 'a');
- fwrite($fp2, $img);
- fclose($fp2);
- unset($img, $url);
- return ['file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0];
- }
-
- public static function zipOpen(string $filename, string $savename)
- {
- $zip = new \ZipArchive;
- $zipfile = $filename;
- $res = $zip->open($zipfile);
- $toDir = $savename;
- if (!file_exists($toDir)) mkdir($toDir, 0777);
- $docnum = $zip->numFiles;
- for ($i = 0; $i < $docnum; $i++) {
- $statInfo = $zip->statIndex($i);
- if ($statInfo['crc'] == 0 && $statInfo['comp_size'] != 2) {
-
- mkdir($toDir . '/' . substr($statInfo['name'], 0, -1), 0777);
- } else {
-
- copy('zip://' . $zipfile . '#' . $statInfo['name'], $toDir . '/' . $statInfo['name']);
- }
- }
- $zip->close();
- return true;
- }
-
- public static function setUtf8($title)
- {
- return iconv('utf-8', 'gb2312', $title);
- }
-
- public static function isWritable($file)
- {
- $file = str_replace('\\', '/', $file);
- if (!file_exists($file)) return false;
- return is_writable($file);
- }
-
- public function readExcel($filePath, $row_num = 2, $suffix = 'Xlsx')
- {
- if (!$filePath) return false;
- $pathInfo = pathinfo($filePath, PATHINFO_EXTENSION);
- if (!$pathInfo || $pathInfo != "xlsx") throw new ValidateException('必须上传xlsx格式文件');
-
- $readModel = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($suffix);
-
-
- try {
- $spreadsheet = $readModel->load($filePath);
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->getHighestColumn();
- $highestRow = $sheet->getHighestRow();
- $lines = $highestRow - 1;
- if ($lines <= 0) {
- throw new ValidateException('物流数据不能为空');
- }
-
-
- $expressService = app()->make(ExpressServices::class);
- $data = $expressCode = [];
- for ($i = $row_num; $i <= $highestRow; $i++) {
- $t1 = $this->objToStr($sheet->getCellByColumnAndRow(1, $i)->getValue());
- $t2 = $this->objToStr($sheet->getCellByColumnAndRow(2, $i)->getValue());
- $t3 = $this->objToStr($sheet->getCellByColumnAndRow(3, $i)->getValue());
- $t4 = $this->objToStr($sheet->getCellByColumnAndRow(4, $i)->getValue());
- $t5 = $this->objToStr($sheet->getCellByColumnAndRow(5, $i)->getValue());
- if ($t1 && $t2 && $t3 && $t4 && $t5) {
- $data[] = [$t1, $t2, $t3, $t4, $t5];
- $expressCode[] = $t4;
- }
- }
- if ($expressCode) {
- $expressCode = array_unique(array_map('trim', $expressCode));
- $expressId = $expressService->getColumn([['code', 'in', $expressCode]], 'id', 'code');
- foreach ($expressCode as $code) {
- if (!isset($expressId[$code])) {
- throw new ValidateException($code . '订单物流公司不在平台收录内');
- }
- }
- }
- return $data;
- } catch (\Exception $e) {
- throw new ValidateException($e->getMessage());
- }
- }
-
- public function readProductCardExcel($filePath, $row_num = 1, $suffix = 'Xlsx')
- {
- if (!$filePath) return false;
- $pathInfo = pathinfo($filePath, PATHINFO_EXTENSION);
- if (!$pathInfo || $pathInfo != "xlsx") throw new ValidateException('必须上传xlsx格式文件');
-
- $readModel = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($suffix);
-
-
- try {
- $spreadsheet = $readModel->load($filePath);
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->getHighestColumn();
- $highestRow = $sheet->getHighestRow();
- $lines = $highestRow - 1;
- if ($lines <= 0) {
- throw new ValidateException('数据不能为空');
- }
-
- $data = [];
- for ($i = $row_num; $i <= $highestRow; $i++) {
- $t1 = $this->objToStr($sheet->getCellByColumnAndRow(1, $i)->getValue()) ?? '';
- $t2 = $this->objToStr($sheet->getCellByColumnAndRow(2, $i)->getValue());
- if ($t2) {
- $data[] = [
- 'key' => $t1,
- 'value' => $t2
- ];
- }
- }
- return $data;
- } catch (\Exception $e) {
- throw new ValidateException($e->getMessage());
- }
- }
-
- public function objToStr($value)
- {
- return is_object($value) ? $value->__toString() : $value;
- }
-
- function addZip($source, $destination, $folder = '')
- {
- if (!extension_loaded('zip') || !file_exists($source)) {
- return false;
- }
- $zip = new \ZipArchive;
- if (!$zip->open($destination, $zip::CREATE)) {
- return false;
- }
- $source = str_replace('\\', '/', $source);
- $folder = str_replace('\\', '/', $folder);
- if (is_dir($source) === true) {
- $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
- foreach ($files as $file) {
- $file = str_replace('\\', '/', $file);
- if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) continue;
- if (is_dir($file) === true) {
- $zip->addEmptyDir(str_replace($folder . '/', '', $file . '/'));
- } else if (is_file($file) === true) {
- $zip->addFromString(str_replace($folder . '/', '', $file), file_get_contents($file));
- }
- }
- } else if (is_file($source) === true) {
- $zip->addFromString(basename($source), file_get_contents($source));
- }
- return $zip->close();
- }
- }
|