Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Upload & Unpack
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="BBC" data-source="post: 179608" data-attributes="member: 27829"><p>Hello guys!</p><p> </p><p>This piece of PHP code will upload a .ZIP file to your server and un-pack it!</p><p> </p><p>Enjoy!</p><p> </p><p>[PHP]<?php</p><p>error_reporting(0);</p><p> </p><p>function rmdir_recursive($dir) {</p><p> foreach(scandir($dir) as $file) {</p><p> if ('.' === $file || '..' === $file) continue;</p><p> if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");</p><p> else unlink("$dir/$file");</p><p> }</p><p> </p><p> rmdir($dir);</p><p>}</p><p> </p><p>if($_FILES["zip_file"]["name"]) {</p><p> $filename = $_FILES["zip_file"]["name"];</p><p> $source = $_FILES["zip_file"]["tmp_name"];</p><p> $type = $_FILES["zip_file"]["type"];</p><p> </p><p> $name = explode(".", $filename);</p><p> $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');</p><p> foreach($accepted_types as $mime_type) {</p><p> if($mime_type == $type) {</p><p> $okay = true;</p><p> break;</p><p> }</p><p> }</p><p> </p><p> $continue = strtolower($name[1]) == 'zip' ? true : false;</p><p> if(!$continue) {</p><p> $message = "The file you are trying to upload is not a .zip file. Please try again.";</p><p>exit;</p><p> }</p><p> </p><p> /* PHP current path */</p><p> $path = dirname(__FILE__).'/'; // absolute path to the directory where zipper.php is in</p><p> $filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase)</p><p> $filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase)</p><p> </p><p> $targetdir = $path . $filenoext; // target directory</p><p> $targetzip = $path . $filename; // target zip file</p><p> </p><p> /* create directory if not exists', otherwise overwrite */</p><p> /* target directory is same as filename without extension */</p><p> </p><p> if (is_dir($targetdir)) rmdir_recursive ( $targetdir);</p><p> </p><p> </p><p> mkdir($targetdir, 0777);</p><p> </p><p> </p><p> /* here it is really happening */</p><p> </p><p> if(move_uploaded_file($source, $targetzip)) {</p><p> $zip = new ZipArchive();</p><p> $x = $zip->open($targetzip); // open the zip file to extract</p><p> if ($x === true) {</p><p> $zip->extractTo($targetdir); // place in the directory with same name</p><p> $zip->close();</p><p> </p><p> unlink($targetzip);</p><p> }</p><p> $message = "Your .zip file was uploaded and unpacked.";</p><p> } else {</p><p> $message = "There was a problem with the upload. Please try again.";</p><p> }</p><p>}</p><p> </p><p> </p><p>?></p><p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></p><p><html xmlns="http://www.w3.org/1999/xhtml"></p><p><head></p><p><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></p><p><title>Unzip a zip file to the webserver</title></p><p></head></p><p> </p><p><body></p><p><?php if($message) echo "<p>$message</p>"; ?></p><p><form enctype="multipart/form-data" method="post" action=""></p><p><label>Choose a zip file to upload: <input type="file" name="zip_file" /></label></p><p><br /></p><p><input type="submit" name="submit" value="Upload" /></p><p></form></p><p></body></p><p></html>[/PHP]</p><p> </p><p>And if you want to upload the file to, for instance "/upload/" find:</p><p>$path = dirname(__FILE__).'/';</p><p>and change it to:</p><p>$path = dirname(__FILE__).'/upload/';</p><p> </p><p>Source(s): <a href="http://stackoverflow.com/questions/6122137/php-upload-and-extract-zip" target="_blank">http://stackoverflow.com/questions/6122137/php-upload-and-extract-zip</a></p></blockquote><p></p>
[QUOTE="BBC, post: 179608, member: 27829"] Hello guys! This piece of PHP code will upload a .ZIP file to your server and un-pack it! Enjoy! [PHP]<?php error_reporting(0); function rmdir_recursive($dir) { foreach(scandir($dir) as $file) { if ('.' === $file || '..' === $file) continue; if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file"); else unlink("$dir/$file"); } rmdir($dir); } if($_FILES["zip_file"]["name"]) { $filename = $_FILES["zip_file"]["name"]; $source = $_FILES["zip_file"]["tmp_name"]; $type = $_FILES["zip_file"]["type"]; $name = explode(".", $filename); $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); foreach($accepted_types as $mime_type) { if($mime_type == $type) { $okay = true; break; } } $continue = strtolower($name[1]) == 'zip' ? true : false; if(!$continue) { $message = "The file you are trying to upload is not a .zip file. Please try again."; exit; } /* PHP current path */ $path = dirname(__FILE__).'/'; // absolute path to the directory where zipper.php is in $filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase) $filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase) $targetdir = $path . $filenoext; // target directory $targetzip = $path . $filename; // target zip file /* create directory if not exists', otherwise overwrite */ /* target directory is same as filename without extension */ if (is_dir($targetdir)) rmdir_recursive ( $targetdir); mkdir($targetdir, 0777); /* here it is really happening */ if(move_uploaded_file($source, $targetzip)) { $zip = new ZipArchive(); $x = $zip->open($targetzip); // open the zip file to extract if ($x === true) { $zip->extractTo($targetdir); // place in the directory with same name $zip->close(); unlink($targetzip); } $message = "Your .zip file was uploaded and unpacked."; } else { $message = "There was a problem with the upload. Please try again."; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Unzip a zip file to the webserver</title> </head> <body> <?php if($message) echo "<p>$message</p>"; ?> <form enctype="multipart/form-data" method="post" action=""> <label>Choose a zip file to upload: <input type="file" name="zip_file" /></label> <br /> <input type="submit" name="submit" value="Upload" /> </form> </body> </html>[/PHP] And if you want to upload the file to, for instance "/upload/" find: $path = dirname(__FILE__).'/'; and change it to: $path = dirname(__FILE__).'/upload/'; Source(s): [URL]http://stackoverflow.com/questions/6122137/php-upload-and-extract-zip[/URL] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Upload & Unpack
Top