How to ftp a zip file and unzip it on the server

Here is what I need to do using php:

  • upload a zip file (e.g. foo.zip) to the server; the zip file contains files and directories
  • unzip the zip file on the server with the owner of the unzipped files and directories ending as ftp user (so they can be later deleted using regular FTP client)

Please note that permissions on the directory where the zip file is uploaded are 755 and the owner of the directory is ftp user.

Here is how I attempted to do it:

  • uploaded the zip file using ‘ftp_put’
  • uploaded ‘unzip.php’ file using ‘ftp_put’
  • called ‘unzip.php’ using curl; ‘unzip.php’ file is using ZipArchive::extractTo to unzip the file. The problem I am having with this approach is that the user the php script is running under do not have ‘write’ permission to the directory so ‘extractTo’ function fails…

Any help will be appreciated.

I faced a similar situation to what you are describing a few months back on a system project that required auto downloading and parsing files. I ended up putting together a small application that did the job.

If you would like I can send you the files and instructions on setup. Hopefully it will be of some use to you.

Thank you for the offer. Is it written in php? If yes I would definitely like to see the code (if possible).

Sure, I will send you a PM with a download link.

vessio’s solution looks good but requires to manually change access permissions on the target server and this is what I am trying to avoid from maintenance and security point of view. The zip file will be uploaded to multiple servers… So for right now the only solution I can think of is to ftp every file in the zip file separately. It will work but will be much slower than 'ftp’ing zip file and unzipping on the target server. so I am still open for any advice.

Another solution is to install ftp server that has post-upload hooks. Basically you can configure the server to execute a script after the upload is complete.
I used pure-ftp server for exactly this type of task.

Well it looks like I am stuck with uploading file by file for right now… so here it the function I created to do it:

function ftp_dir_upload($conn,$path2dir){
	$dir = dir($path2dir);
	while(($file = $dir->read()) !== false) {
		
		if(is_dir($dir->path.$file)) {
			if (($file == '.') or ($file == '..')) {
				continue;
			}
			if (!ftp_mkdir($conn,$file)) {
				$dir->close();
				return false;
			}
			if (!ftp_chdir($conn,$file)) {
				$dir->close();
				return false;
			}
			
			ftp_dir_upload($conn,$dir->path.$file.'/');

			if (!ftp_chdir($conn,'..')) {
				$dir->close();
				return false;
			}
		} else {
			if (!ftp_put($conn,$file,$dir->path.$file,FTP_BINARY)) {
				$dir->close();
				return false;
			}
		}
	}
	$dir->close();
	return true;
}

It is pretty slow because I need to transfer 726 files…

Good suggestion… The only problem is that both the base and target servers are out of my control. Scripts are running on hosted accounts.

You can try Wing FTP Server, it supports zip/unzip on the server side perfectly.

And you can download it from: www.wftpserver.com/download.htm