How to Upload and Extract a ZIP File to the Server

Hey,

Any ideas how i can upload a zip file containing images? I need a method where in the backend admin section of a website i can upload a zip file and extract the contents of it inside a folder called “images”…

Can this be done?

Are there any examples, tutorials etc…

Thanks again

I’m trying to have a crack at some code i have bee following, don’t know if this is in any way correct :slight_smile: but lets see what you guys think. So i need to upload and extract the contents of a ZIP file to a folder on the server called “reviewimages”…

This is my method:


            public function UploadZIPFileToServer(){
		
		move_uploaded_file($_FILES["zipfile"]["tmp_name"], "../reviewimages/" . $_FILES["zipfile"]["name"]);
		
		if(is_uploaded_file($_FILES['zipfile']['tmp_name'])){
		
			$zipfilename = basename($_FILES['zipfile']['name']);  
			$zip = zip_open($zipfilename);
				
			if ($zip) {
				while ($zip_entry = zip_read($zip)) {
				$fp = fopen($targetdir . zip_entry_name($zip_entry), "w");
					if (zip_entry_open($zip, $zip_entry, "r")) {
						$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
						fwrite($fp,"$buf");
						zip_entry_close($zip_entry);
						fclose($fp);
					}
				}
			}
			zip_close($zip);   
		}
	}

And this is how i fire the method:


if(isset($_POST['uploadZIP'])){
	$message = Review::UploadZIPFileToServer();
    echo '<script type="text/javascript">window.location = "http://'.$_SERVER['SERVER_NAME'].$sitename.'manage-reviews.php"</script>';                
}

			<form enctype="multipart/form-data" action="" method="post" id="add-courses">
			    <table>
			        <tr>
			        	<td class="width"><label for="image">Upload ZIP file : </label></td>
			        	<td><input type="hidden" name="MAX_FILE_SIZE" value="1000000000000" /><input type="file" name="zipfile" id="zipfile" value=""/></td>
						<td><input type="submit" name="uploadZIP" value="Upload" /></td>
					</tr>
				</table>
			</form>

This uploads the ZIP file to the server into the “reviewimages” folder, but i need it to extract its contents which it does not do… Any ideas?

Thanks

Sure, handle the upload as normal then use PHP’s Zip functionality.

However, don’t go ahead skipping the usual security aspects either, they’re just as relevant (if not worse) with Zip files.

:slight_smile: