提取zip而不创建新文件夹

时间:2015-03-26 12:23:44

标签: php ziparchive

目前如果我用(myfolder.zip)上传一个zip,那么它会提取并创建一个文件夹myfolder / image1.jpeg我需要它应该像root1 / jpeg一样在root上提取内容。

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文件时,它会被上传,然后在上传文件夹中被提取,如上传/ abcd / image1.jpeg我需要它不应该有abcd upload / image1.jpeg

1 个答案:

答案 0 :(得分:0)

试试这个:

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