FTP上传后PHP解压缩

时间:2016-03-16 02:52:34

标签: php ftp unzip

我有这些代码行通过FTP上传文件。上传后,我需要解压缩文件。遇到的问题是,文件已成功上传但我无法解压缩。谁能帮我这个?

if(isset($_POST['submit'])){  
    $file = $_FILES['uploaded_file']['name'];
    $remote_file = $_FILES['uploaded_file']['name'];

    $ftp_server = "1xx.xx.xx.xx";
    $ftp_user_name = "xxuser";
    $ftp_user_pass = "xx2016";
    $toform2 = "FormType/Upload/";
    $tounzip2 = "Unzip/";

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

    // login
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    ftp_pasv($conn_id, true);

    // upload the file 
    $ftype = substr ($file,0,2);
    if ($ftype == "F2") {
       ftp_chdir ($conn_id,$toform2);
       $upload = ftp_put($conn_id,$remote_file,$file,FTP_ASCII); 
       $file_path = $toform2;

      // check upload status 
      if($upload){ 
        // Unzip file
        $zip = new ZipArchive();            
        $x = $zip->open($toform2);
        if ($x === true) {
            ftp_chdir ($conn_id,$tounzip2);
            $zip->extractTo($tounzip2); 
            $zip->close();
            echo "success"."</br>";
         }else{
            echo "fail";
        }
        //echo "Uploaded $source_file to $ftp_server as $destination_file" ;
    }else{ 
        //echo "FTP upload has failed!" ;  
    } 


}

1 个答案:

答案 0 :(得分:0)

如果脚本具有对远程服务器的SSH访问权限,则可以使用ssh2-exec远程脚本来解压缩文件。

SSH2-连接: http://php.net/manual/en/function.ssh2-connect.php

SSH2-Exec的: http://php.net/manual/en/function.ssh2-exec.php

首先使用FTP上传文件。 (SFTP会更好)

打开SSH连接并远程执行unzip命令。