This commit is contained in:
mkm 2023-11-29 22:55:47 +08:00
parent 3f9960bf4b
commit 7cbee24d8d
2 changed files with 122 additions and 28 deletions

View File

@ -31,33 +31,41 @@ class Demo extends BaseController
public function index() public function index()
{ {
$mer_id = 65; $mer_id = 65;
$file = request()->file('file'); $file = request()->file('file');
$zip_name = explode('.', $file->getOriginalName())[0]; $zip_name = explode('.', $file->getOriginalName())[0];
// 上传到本地服务器 // 上传到本地服务器
$savename = \think\facade\Filesystem::putFile('zippic', $file); $savename = \think\facade\Filesystem::putFile('zippic', $file);
$dir = date('Y-m-d_H-i-s') . '_' . $mer_id; $dir = date('Y-m-d_H-i-s') . '_' . $mer_id;
$destination_path = public_path('uploads/pic/' . $dir.'_'.$zip_name); $path = public_path('uploads/pic').$dir;
mkdir($destination_path, 0777, true);
$zipFile = new \PhpZip\ZipFile();
try { try {
$zipFile $zip = new ZipArchive;
->openFile(public_path('uploads').$savename) // open archive from file $filePath = public_path('uploads') . $savename;
->extractTo($destination_path) // add an entry from the string $zip->open($filePath);
// ->deleteFromRegex('~^\.~') // delete all hidden (Unix) files for ($i = 0; $i < $zip->numFiles; $i++) {
->close(); // close archive $statInfo = $zip->statIndex($i, ZipArchive::FL_ENC_RAW);
} catch (\PhpZip\Exception\ZipException $e) { $filename = $this->transcoding($statInfo['name']);
throw new \think\exception\HttpException(404,$e->getMessage()); $mkdFile = explode('/',$filename);
} finally { if ($statInfo['crc'] == 0) {
$zipFile->close(); // 新建目录
} if (!file_exists($path . '/' . $filename)) {
mkdir($path . '/' . $filename, 0777, true);
}
// $this->createDirectories($path . '/' . $filename);
} else {
// 拷贝文件
if(count($mkdFile)==3){
if (!file_exists($path . '/' . $mkdFile[0].'/'.$mkdFile[1])) {
mkdir($path . '/' .$mkdFile[0].'/'.$mkdFile[1], 0777, true);
}
copy('zip://' . $file . '#' . $zip->getNameIndex($i), $path . '/' . $filename);
// if ($zip->open(public_path('uploads').$savename) === TRUE) { }
// $zip->extractTo($destination_path); }
// $zip->close(); }
// } else { $zip->close();
// throw new \think\exception\HttpException(404, '解压失败'); } catch (\Exception $e) {
// } throw new \think\exception\HttpException(404, $e->getMessage() . '。line:' . $e->getLine());
}
$directory = $destination_path; $directory = $destination_path;
$files = scandir($directory); $files = scandir($directory);
$dir = 'def/' . date('Y-m-d'); $dir = 'def/' . date('Y-m-d');
@ -161,4 +169,38 @@ class Demo extends BaseController
} }
halt(1); halt(1);
} }
public function transcoding($fileName)
{
$encoding = mb_detect_encoding($fileName, ['UTF-8', 'GBK', 'BIG5', 'CP936']);
if (DIRECTORY_SEPARATOR == '/') { // linux
$fileName = iconv($encoding, 'UTF-8', $fileName);
} else { // win
$fileName = iconv($encoding, 'GBK', $fileName);
}
return $fileName;
}
function createDirectories($path) {
if (is_dir($path)) {
return true ;
} else {
$parts = explode(DIRECTORY_SEPARATOR, $path);
$part = null;
foreach ($parts as $part) {
if ($part === '' || $part === '.' || $part === '..') {
continue;
}
$dir = dirname($path) . DIRECTORY_SEPARATOR . $part;
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
}
return true ;
}
}
} }

View File

@ -172,16 +172,36 @@ class StoreImport extends BaseController
// 上传到本地服务器 // 上传到本地服务器
$savename = \think\facade\Filesystem::putFile('zippic', $file); $savename = \think\facade\Filesystem::putFile('zippic', $file);
$mer_id = $this->request->merId(); $mer_id = $this->request->merId();
$dir=date('Y-m-d_H-i-s').'_'.$mer_id; $dir = date('Y-m-d_H-i-s') . '_' . $mer_id;
$destination_path = public_path('uploads/pic/'.$dir) . $zip_name; $path = public_path('uploads/pic').$dir;
$zip = new ZipArchive; try {
if ($zip->open(public_path('uploads').$savename) === true) { $zip = new ZipArchive;
$zip->extractTo($destination_path); $filePath = public_path('uploads') . $savename;
$zip->open($filePath);
for ($i = 0; $i < $zip->numFiles; $i++) {
$statInfo = $zip->statIndex($i, ZipArchive::FL_ENC_RAW);
$filename = $this->transcoding($statInfo['name']);
$mkdFile = explode('/',$filename);
if ($statInfo['crc'] == 0) {
// 新建目录
if (!file_exists($path . '/' . $filename)) {
mkdir($path . '/' . $filename, 0777, true);
}
} else {
// 拷贝文件
if(count($mkdFile)==3){
if (!file_exists($path . '/' . $mkdFile[0].'/'.$mkdFile[1])) {
mkdir($path . '/' .$mkdFile[0].'/'.$mkdFile[1], 0777, true);
}
copy('zip://' . $file . '#' . $zip->getNameIndex($i), $path . '/' . $filename);
}
}
}
$zip->close(); $zip->close();
} else { } catch (\Exception $e) {
throw new \think\exception\HttpException(404, '解压失败'); throw new \think\exception\HttpException(404, $e->getMessage() . '。line:' . $e->getLine());
} }
Queue::push(ImportPicJob::class,['mer_id'=>$mer_id,'path'=>$destination_path]); Queue::push(ImportPicJob::class,['mer_id'=>$mer_id,'path'=>$path.'/'.$zip_name]);
return app('json')->success('开始导入数据,请稍后在列表中查看!,如果未导入请检查格式'); return app('json')->success('开始导入数据,请稍后在列表中查看!,如果未导入请检查格式');
} }
@ -241,5 +261,37 @@ class StoreImport extends BaseController
app()->make(StoreOrderRepository::class)->setProduct($data['data'],$data['mer_id'],$type_id); app()->make(StoreOrderRepository::class)->setProduct($data['data'],$data['mer_id'],$type_id);
} }
} }
public function transcoding($fileName)
{
$encoding = mb_detect_encoding($fileName, ['UTF-8', 'GBK', 'BIG5', 'CP936']);
if (DIRECTORY_SEPARATOR == '/') { // linux
$fileName = iconv($encoding, 'UTF-8', $fileName);
} else { // win
$fileName = iconv($encoding, 'GBK', $fileName);
}
return $fileName;
}
function createDirectories($path) {
if (is_dir($path)) {
return true ;
} else {
$parts = explode(DIRECTORY_SEPARATOR, $path);
$part = null;
foreach ($parts as $part) {
if ($part === '' || $part === '.' || $part === '..') {
continue;
}
$dir = dirname($path) . DIRECTORY_SEPARATOR . $part;
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
}
return true ;
}
}
} }