-1

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.

user4676307
  • 409
  • 8
  • 22

2 Answers2

1

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

RaMeSh
  • 3,330
  • 2
  • 19
  • 31
0

You can use the ZipArchive class for this

$archive = new ZipArchive;
if ($archive->open('my_zip_fil.zip') === TRUE) {
    $archive->extractTo('destination/dir');
    $archive->close();
}

ZipArchive documentation

Stefan Schmidt
  • 1,152
  • 11
  • 18
  • Hi thank you for your help but I tried to use ziparchive and it wont extract the file for some reason or giving me any errors. – user4676307 Apr 08 '15 at 10:29