Search code examples
phpunzip

PHP Unzip Local File


Hi I need to download a zip file using ftp and then unzip it on local computer. I can download the file using ftp_get but I need help on unzipping it to specific location.

        $ftpServer = "server";
        $ftpUser = "user";
        $ftpPassword = "password";

        $ftp_server = $ftpServer;
        $ftp_conn = ftp_connect($ftp_server);
        $ftp_login = ftp_login($ftp_conn, $ftpUser, $ftpPassword ); 

        if(!$ftp_conn) 
            die("A connection to $ftpServer couldn't be established"); 
        else if(!$ftp_login) 
            die("Your login credentials were rejected"); 
        else
        {
            $file = "C:/files/downloads/FILE_NAME.ZIP";

            $server_file = "/FILE_NAME.ZIP"; 

            if (ftp_get($ftp_conn, $file, $server_file, FTP_ASCII)) 
            {

                $zip = new ZipArchive;
                $res = $zip->open($file);

                if ($res === TRUE) 
                {
                  $zip->extractTo('C:/files/Feeds/');
                  $zip->close();
                }

                unlink($file);
            }
            else 
            {
                echo "There was a problem\n";
                $this->index();
            }
        }

Thank you for you help.


Solution

  • Try with this code

    Unzip.php:-

    <?php
    $zip = new ZipArchive;
    if ($zip->open('/sites/gallery.zip') === TRUE) {
        $zip->extractTo('/sites/New folder/test');
        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }
    ?>
    

    For your reference see this link Documentation