PHP解压缩本地文件

时间:2015-04-08 10:11:50

标签: php unzip

您好我需要使用ftp下载zip文件,然后在本地计算机上解压缩。我可以使用ftp_get下载该文件,但我需要帮助将其解压缩到特定位置。

        $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();
            }
        }

谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

尝试使用此代码

<强> 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';
}
?>

供参考,请参阅此链接Documentation

答案 1 :(得分:0)

您可以将ZipArchive类用于此

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

ZipArchive documentation