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

JavaScript

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

    JavaScript
  • And then you try to open /htdocs/file.zip, as it it were a local file

    JavaScript

    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.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement