用php解压文件

我想解压缩文件,这工作正常

system('unzip File.zip'); 

但是我需要通过URL传递文件名,不能让它工作,这是我的。

 $master = $_GET["master"]; system('unzip $master.zip'); 

我错过了什么? 我知道它必须是一个小而笨的我忽略的东西。

谢谢,

我只能假设你的代码来自一个在线的教程? 在那种情况下,好好努力自己搞清楚。 另一方面,这个代码实际上可以在网上发布的事实是解压文件的正确方法,这有点可怕。

PHP有内置的处理压缩文件的扩展。 应该没有必要使用system调用。 ZipArchive 文档是一个选项。

 $zip = new ZipArchive; $res = $zip->open('file.zip'); if ($res === TRUE) { $zip->extractTo('/myzips/extract_path/'); $zip->close(); echo 'woot!'; } else { echo 'doh!'; } 

另外,正如其他人所说的那样, $HTTP_GET_VARS从4.1版本开始已经被弃用了,这是很久以前的事了。 不要使用它。 改用$_GET superglobal。

最后,要非常小心地接受通过$_GETvariables传递给脚本的任何input。

总是SANITIZE用户input。


UPDATE

根据您的评论,将zip文件解压缩到其所在的同一目录中的最佳方式是确定文件的硬path并将其专门提取到该位置。 所以,你可以这样做:

 // assuming file.zip is in the same directory as the executing script. $file = 'file.zip'; // get the absolute path to $file $path = pathinfo(realpath($file), PATHINFO_DIRNAME); $zip = new ZipArchive; $res = $zip->open($file); if ($res === TRUE) { // extract it to the path we determined above $zip->extractTo($path); $zip->close(); echo "WOOT! $file extracted to $path"; } else { echo "Doh! I couldn't open $file"; } 

请不要这样做(传递GET var作为系统调用的一部分)。 改用ZipArchive 。

所以,你的代码应该是这样的:

 $zipArchive = new ZipArchive(); $result = $zipArchive->open($_GET["master"]); if ($result === TRUE) { $zipArchive ->extractTo("my_dir"); $zipArchive ->close(); // Do something else on success } else { // Do something on error } 

而要回答你的问题,你的错误是'某个$ var别的东西'应该是“$ var其他东西”(用双引号)。

只需试试这个的目标目录是提取或去除-d yourDestinationDir提取到根目录的目的地。

 $master = 'someDir/zipFileName'; $data = system('unzip -d yourDestinationDir '.$master.'.zip'); 

我更新了@rdlowrey的答案, 使其更清晰和更好的代码,这将使用__DIR__将文件解压缩到当前目录中。

 <?php // config // ------------------------------- // only file name + .zip $zip_filename = "YOURFILENAME.zip"; ?> <!DOCTYPE html> <html> <head> <meta charset='utf-8' > <title>Unzip</title> <style> body{ font-family: arial, sans-serif; word-wrap: break-word; } .wrapper{ padding:20px; line-height: 1.5; font-size: 1rem; } span{ font-family: 'Consolas', 'courier new', monospace; background: #eee; padding:2px; } </style> </head> <body> <div class="wrapper"> <?php echo "Unzipping <span>" .__DIR__. "/" .$zip_filename. "</span> to <span>" .__DIR__. "</span><br>"; echo "current dir: <span>" . __DIR__ . "</span><br>"; $zip = new ZipArchive; $res = $zip->open(__DIR__ . '/' .$zip_filename); if ($res === TRUE) { $zip->extractTo(__DIR__); $zip->close(); echo '<p style="color:#00C324;">Extract was successful! Enjoy ;)</p><br>'; } else { echo '<p style="color:red;">Zip file not found!</p><br>'; } ?> End Script. </div> </body> </html> 

我把Morteza Ziaeemehr的答案更新为一个更干净更好的代码,这将使用DIR将表单中提供的文件解压缩到当前目录中。

 <!DOCTYPE html> <html> <head> <meta charset='utf-8' > <title>Unzip</title> <style> body{ font-family: arial, sans-serif; word-wrap: break-word; } .wrapper{ padding:20px; line-height: 1.5; font-size: 1rem; } span{ font-family: 'Consolas', 'courier new', monospace; background: #eee; padding:2px; } </style> </head> <body> <div class="wrapper"> <?php if(isset($_GET['page'])) { $type = $_GET['page']; global $con; switch($type) { case 'unzip': { $zip_filename =$_POST['filename']; echo "Unzipping <span>" .__DIR__. "/" .$zip_filename. "</span> to <span>" .__DIR__. "</span><br>"; echo "current dir: <span>" . __DIR__ . "</span><br>"; $zip = new ZipArchive; $res = $zip->open(__DIR__ . '/' .$zip_filename); if ($res === TRUE) { $zip->extractTo(__DIR__); $zip->close(); echo '<p style="color:#00C324;">Extract was successful! Enjoy ;)</p><br>'; } else { echo '<p style="color:red;">Zip file not found!</p><br>'; } break; } } } ?> End Script. </div> <form name="unzip" id="unzip" role="form"> <div class="body bg-gray"> <div class="form-group"> <input type="text" name="filename" class="form-control" placeholder="File Name (with extension)"/> </div> </div> </form> <script type="application/javascript"> $("#unzip").submit(function(event) { event.preventDefault(); var url = "function.php?page=unzip"; // the script where you handle the form input. $.ajax({ type: "POST", url: url, dataType:"json", data: $("#unzip").serialize(), // serializes the form's elements. success: function(data) { alert(data.msg); // show response from the php script. document.getElementById("unzip").reset(); } }); return false; // avoid to execute the actual submit of the form }); </script> </body> </html> 

只是改变

 system('unzip $master.zip'); 

对这个

 system('unzip ' . $master . '.zip'); 

或这一个

system("unzip {$master}.zip");

你可以使用预装function

 function unzip_file($file, $destination){ // create object $zip = new ZipArchive() ; // open archive if ($zip->open($file) !== TRUE) { return false; } // extract contents to destination directory $zip->extractTo($destination); // close archive $zip->close(); return true; } 

如何使用它。

 if(unzip_file($file["name"],'uploads/')){ echo 'zip archive extracted successfully'; }else{ echo 'zip archive extraction failed'; } 
 function extract_zip($Sourse_file, $extract_folder){ $zip = new ZipArchive() ; if (!$zip->open($Sourse_file) == TRUE) { return false; } $zip->extractTo($extract_folder); $zip->close(); return true; } 

使用下面的PHP代码,文件名在URL参数“名称”

 <?php $fileName = $_GET['name']; if (isset($fileName)) { $zip = new ZipArchive; $res = $zip->open($fileName); if ($res === TRUE) { $zip->extractTo('./'); $zip->close(); echo 'Extracted file "'.$fileName.'"'; } else { echo 'Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))'; } } else { echo 'Please set file name in the "name" param'; } ?> 

PHP有自己的内置类,可以用来解压缩或从zip文件中提取内容。 这个类是ZipArchive。 下面是简单和基本的PHP代码,将提取一个zip文件,并将其放在一个特定的目录中:

 <?php $zip_obj = new ZipArchive; $zip_obj->open('dummy.zip'); $zip_obj->extractTo('directory_name/sub_dir'); ?> 

如果你想要一些高级function,那么下面是改进后的代码,它将检查zip文件是否存在:

 <?php $zip_obj = new ZipArchive; if ($zip_obj->open('dummy.zip') === TRUE) { $zip_obj->extractTo('directory/sub_dir'); echo "Zip exists and successfully extracted"; } else { echo "This zip file does not exists"; } ?> 

来源: 如何在PHP中解压缩或提取压缩文件?

只需使用这个:

  $master = $_GET["master"]; system('unzip' $master.'.zip'); 

在你的代码中, $master作为string传递,系统将寻找名为$master.zip的文件

  $master = $_GET["master"]; system('unzip $master.zip'); `enter code here`