【发布时间】:2015-03-26 12:23:43
【问题描述】:
目前,如果我使用 (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
【问题讨论】:
-
那你有什么问题?
-
目前上面的代码可以工作,但它会创建一个新文件夹,我只想解压缩而不创建任何目录。
-
那么
$args["path"]的值是多少,你想把提取的文件放在哪里? -
$args['data'] = $this->request->data['Uploadzip']['zip_file']; $args['path'] = WWW_ROOT.'upload/';让我解释一下,当我上传一个名为 abcd.zip 的 zip 文件时,它会被上传,然后被提取到像这样的上传文件夹中 upload/abcd/image1.jpeg 我需要它不应该有 abcd upload/image1.jpeg
-
在php docs page for extractTo()php docs page for extractTo()@proneticas dot net 上阅读 php-dev 的用户贡献注释
标签: php ziparchive