Skip to content
Advertisement

Upload a ZIP file and UNZIP ftp folder via PHP

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 $filen";
}

// 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:xampphtdocsupload.php on line 29

Warning: ZipArchive::close(): Invalid or uninitialized Zip object in C:xampphtdocsupload.php on line 30

Advertisement

Answer

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 a ZIP file on an FTP 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.


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);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement