Upload a ZIP file and UNZIP ftp folder via PHP

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-03 09:09:09

问题


I want to make a form where you can fill FTP login server and get option to upload ZIP file. The script works apart from the last part (UNZIP the file) I want to perform UNZIP uploaded file. Does anyone know what is the problem? TIA

<?php 
if (isset($_POST['Submit'])) {
$ftp_server = $ftp = $_POST['ftp'];
$ftp_user_name = $username = $_POST['username'];
$ftp_user_pass = $password = $_POST['password'];   


 if (!empty($_FILES['upload']['name'])) {
$ch = curl_init();

$file1 = $localfile = $_FILES['upload']['name'];
$fp = fopen($file1, 'r');

$file = '/htdocs/file.zip';
// 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 upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
    echo "Successfully uploaded ftp://".$username.":".$password."@".$ftp.$file."\n";

    $zip = new ZipArchive;
    $zip->open($file); 
    $zip->extractTo('ftp://'.$username.':'.$password.'@'.$ftp.'/htdocs'); 
    $zip->close();

} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
 }  
}
?>

    <?php if(isset($error)){ echo $error; } ?>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <div>
                <label for="upload">Select file</label>
                <input name="upload" type="file" />
                <br> Ftp Server:
                <br>
                <input type="text" name="ftp" value="<?php if(isset($ftp)){ echo $ftp; } ?>">
                <br> Username:
                <br>
                <input type="text" name="username" value="<?php if(isset($username)){ echo $username; } ?>">
                <br> Password:
                <br>
                <input type="text" name="password" value="<?php if(isset($password)){ echo $password; }else{ echo '123456';} ?>">

                <input type="submit" name="Submit" value="Upload" />
            </div>
        </form>

THE ERROR

Successfully uploaded

ftp://:@ftp.***.com/htdocs/file.zip Warning: ZipArchive::extractTo(): Invalid or uninitialized Zip object in C:\xampp\htdocs\upload.php on line 29

Warning: ZipArchive::close(): Invalid or uninitialized Zip object in C:\xampp\htdocs\upload.php on line 30


回答1:


The ZipArchive does not support URL wrappers.

And your code does not make much sense anyway:

  • You first upload $localfile to FTP server as /htdocs/file.zip

    $file = '/htdocs/file.zip';
    ftp_fput($conn_id, $file, $fp, FTP_ASCII)
    
  • And then you try to open /htdocs/file.zip, as it it were a local file

    $zip->open($file);
    

    But such local file does not exists.

  • And then you try to extract that non existing file to FTP URL. And that's not supported.

    See ZipArchive::open(): support stream wrappers. It's about open, but if open does not support wrappers, the extactTo won't either (it's a way more difficult to support). See a comment by cmb:

    Anyhow, ZipArchive::open() is not supposed to accept any stream wrapper URLs, but only real file paths. Its documentation doesn't tell otherwise, and neither does the man page on "Supported Protocols and Wrappers"[1]:

    | PHP comes with many built-in wrappers for various URL-style | protocols for use with the filesystem functions [...]

    However, ZipArchive::open() is not a filesystem function for that matter.

    So, actually, this is not a bug, not even a documentation bug in the strict sense. Therefore I'm changing to feature request.


As your code is just wrong, its difficult to guess, what you are actually trying to do. I can imagine these two possibilities.

  • You wanted to upload a ZIP file to FTP server and unzip it there. It's simply not possible to unzip ZIP file on the ZIP server.

  • You wanted to extract a local ZIP to FTP server. While it may seem that it's possible with use of URL wrapper in the ZipArchive:extractTo call, it's not. As I've shown above. Nor there is any other way to extract local ZIP file to FTP server with some simple one-liner in PHP.

    All you can do, is to extract the ZIP file locally (on the web server); and then upload it file-by-file to the FTP server.

    • Create a temporary folder and extract (ZipArchive::extractTo) the ZIP archive there.

    • Upload the temporary folder to the FTP server.

      See PHP - ftp_put + Copy Entire Folder Structure.

    • Delete the temporary folder.


Also note that you upload the file using ASCII mode. A ZIP format is binary. By uploading binary file in ASCII mode, you damage it.

ftp_fput($conn_id, $file, $fp, FTP_ASCII);



回答2:


Actually none of the FTP clients can extract or unzip archive files on the FTP server because there is no such command in FTP. However, net2ftp makes it possible by downloading the archive file from your web server, extracts the files in their server and then upload all the files back to your server. Hence, you might notice that it takes quite a long time for net2ftp to complete uploading if there are thousands of files in the archive.



来源:https://stackoverflow.com/questions/40506789/upload-a-zip-file-and-unzip-ftp-folder-via-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!