上传ZIP文件并在服务器上解压的脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传ZIP文件并在服务器上解压的脚本相关的知识,希望对你有一定的参考价值。
Easy php script for uploading and unpacking zip files to the server much fasterSteps to follow:
1. create folder on the webserver called "zipper" or something else
2. copy the following PHP code into a file called "zipper.php" into this folder
3. use the url to this file to start the little script
4. select a zip file from your harddisk and go for it
5. after upload a directory exists in the folder "zipper" from step 1
with ftp program (like Filezilla) you can easily move the content to the directory of your choice (that's all), which goes much faster
<?php /* Simple script to upload a zip file to the webserver and have it unzipped Saves tons of time, think only of uploading Wordpress to the server Thanks to c.bavota (www.bavotasan.com) I have modified the script a little to make it more convenient Modified by: Johan van de Merwe (12.02.2013) */ function rmdir_recursive($dir) { if ('.' === $file || '..' === $file) continue; } } if($_FILES["zip_file"]["name"]) { $filename = $_FILES["zip_file"]["name"]; $source = $_FILES["zip_file"]["tmp_name"]; $type = $_FILES["zip_file"]["type"]; $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; } } if(!$continue) { $message = "The file you are trying to upload is not a .zip file. Please try again."; } /* PHP current path */ $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 */ /* here it is really happening */ $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(); } $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>
以上是关于上传ZIP文件并在服务器上解压的脚本的主要内容,如果未能解决你的问题,请参考以下文章