提取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.'上传/';

当我上传一个名为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;
        } 
    }