在不创建新文件夹的情况下提取 zip
Extract zip without creating new folder
目前,如果我使用 (myfolder.zip) 上传一个 zip,那么它会提取并创建一个文件夹 myfolder/image1.jpeg 我需要它应该像 image1/jpeg 一样提取根目录中的内容。
function uploadzip($args){
$message['flag'] = false;
$message['message'] = "There was a problem with the upload. Please try again.";
if($args["data"]["name"]) {
$filename = $args["data"]["name"];
$source = $args["data"]["tmp_name"];
$type = $args["data"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = $args["path"].$filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($args["path"]); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message['message'] = "Your .zip file was uploaded and unpacked.";
$message['flag'] =true;
}
return $message;
}
}
$args['data'] = $this->request->data['Uploadzip']['zip_file'];
$args['path'] = WWW_ROOT.'upload/';
当我上传一个名为 abcd.zip 的 zip 文件时,它会被上传,然后被提取到上传文件夹中,就像这样 upload/abcd/image1.jpeg 我需要它不应该有 abcd upload/image1.jpeg
试试这个:
function uploadzip($args){
$message['flag'] = false;
$message['message'] = "There was a problem with the upload. Please try again.";
if($args["data"]["name"]) {
$filename = $args["data"]["name"];
$source = $args["data"]["tmp_name"];
$type = $args["data"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = $filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($args["path"]); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message['message'] = "Your .zip file was uploaded and unpacked.";
$message['flag'] =true;
}
return $message;
}
}
目前,如果我使用 (myfolder.zip) 上传一个 zip,那么它会提取并创建一个文件夹 myfolder/image1.jpeg 我需要它应该像 image1/jpeg 一样提取根目录中的内容。
function uploadzip($args){
$message['flag'] = false;
$message['message'] = "There was a problem with the upload. Please try again.";
if($args["data"]["name"]) {
$filename = $args["data"]["name"];
$source = $args["data"]["tmp_name"];
$type = $args["data"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = $args["path"].$filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($args["path"]); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message['message'] = "Your .zip file was uploaded and unpacked.";
$message['flag'] =true;
}
return $message;
}
}
$args['data'] = $this->request->data['Uploadzip']['zip_file']; $args['path'] = WWW_ROOT.'upload/';
当我上传一个名为 abcd.zip 的 zip 文件时,它会被上传,然后被提取到上传文件夹中,就像这样 upload/abcd/image1.jpeg 我需要它不应该有 abcd upload/image1.jpeg
试试这个:
function uploadzip($args){
$message['flag'] = false;
$message['message'] = "There was a problem with the upload. Please try again.";
if($args["data"]["name"]) {
$filename = $args["data"]["name"];
$source = $args["data"]["tmp_name"];
$type = $args["data"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = $filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($args["path"]); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message['message'] = "Your .zip file was uploaded and unpacked.";
$message['flag'] =true;
}
return $message;
}
}