【问题标题】:Extract zip without creating new folder在不创建新文件夹的情况下提取 zip
【发布时间】: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


【解决方案1】:

试试这个:

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;
        } 
    }

【讨论】:

  • 我试过了,但还是一样,它仍然会创建目录然后解压到那里,我希望它应该直接解压而不创建文件夹
  • $args["path"] 的值是多少?
  • $args["path"] = /app/webroot/upload/
  • 替换此行 "$target_path = $args["path"].$filename;"用这个 $target_path = $args["path"].$name[0].'.'$name[1];告诉我结果
  • #ahmed Parse 错误:语法错误,意外的'$name' (T_VARIABLE) in
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多