使用php解压缩BZ2(__HALT_COMPILER();未找到)

时间:2017-05-24 03:24:41

标签: php

我尝试用php脚本解压缩bz2文件,但我有这个错误:

  

PHP致命错误:未捕获UnexpectedValueException:internal   贪污腐败   “/Users/apple/projects/asystem/database/dump/xxxx.sql.bz2”   (__HALT_COMPILER();未找到)   /Users/apple/projects/asystem/database/dump/dump_auctions.php:33堆栈   跟踪:   0 /Users/apple/projects/asystem/database/dump/dump_auctions.php(33):PharData-> __ construct('auct_auctions_x ...')   在线上/Users/apple/projects/asystem/database/dump/dump_auctions.php中输入1个{main}   33

我想要实现的是:从FTP下载文件,解压缩并导入到数据库。但我坚持解压缩 bz2

这是我的代码:

    <?php

    // Download Auctions dump

    // define some variables
    $local_file = "xxxx.sql.bz2";
    $server_file = "xxxx.sql.bz2";

    //-- Connection Settings
    $ftp_server = "xxxxx.xxxxx.com"; // Address of FTP server.
    $ftp_user_name = "xxxxxx_user"; // Username
    $ftp_user_pass = "xxxxx"; // Password


    // set up basic connection
    $conn_id = ftp_connect($ftp_server);

    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

    // try to download $server_file and save to $local_file
    if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
        echo "Successfully written to $local_file\n";
    } else {
        echo "There was a problem\n";
    }

    // close the connection
    ftp_close($conn_id);

    // unzip file
    $p = new PharData('xxxx.sql.bz2');
    $p->decompress(); 
?>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案here

刚刚使用了这段代码:

// set input and output files
$out = 'xxxx.sql';
$in = 'xxxx.sql.bz2';

// decompress file using BZIP2
if (file_exists($in)) {
    $data = '';
    $bz = bzopen($in, 'r') or die('ERROR: Cannot open input file!');
    while (!feof($bz)) {
        $data .= bzread($bz, 4096) or die('ERROR: Cannot read from input file');;
    }
    bzclose($bz);
    file_put_contents($out, $data) or die('ERROR: Cannot write to output file!');
    echo 'Decompression complete.';
}