Search code examples
phpphp-ziparchive

Extract zip without creating new folder


Currently If I upload a zip with (myfolder.zip) then it extract and create a folder myfolder/image1.jpeg I need it should extract the content on root like 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/';

When I upload a zip file named abcd.zip it gets uploaded and then gets extracted in upload folder like this upload/abcd/image1.jpeg I need it should not have abcd upload/image1.jpeg


Solution

  • Try with this:

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