php 上传 Zip 文件并解压缩 Zip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22355653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:51:55  来源:igfitidea点击:

Upload Zip File and Extract the Zip

phpfile-uploadzipextract

提问by Cody Raspien

I have a form (HTML) which submits a file to a PHP script which renames the file to ZIP, stores it in a folder (random name) and then extracts that file.

我有一个表单 (HTML),它将文件提交给 PHP 脚本,该脚本将文件重命名为 ZIP,将其存储在一个文件夹中(随机名称),然后提取该文件。

The file gets uploaded. The folder is created correctly. The file gets renamed correctly.

文件被上传。该文件夹已正确创建。文件被正确重命名。

The extraction of the zip fails.

zip 的提取失败。

Here is my form:

这是我的表格:

<form action="up.php" method="post" enctype="multipart/form-data" name="form1" id="form1">

Select file 
<input name="ufile" type="file" id="ufile" size="50" />

<input type="submit" name="Submit" value="Upload" />

 </form>

Here is the PHP script - up.php

这是 PHP 脚本 - up.php

$file_name = $HTTP_POST_FILES['ufile']['name'];
$random_digit=rand(0000,9999);
$new_file_name=$random_digit.".zip";
mkdir($random_digit, 0777, true);

$path= $random_digit.'/'.$new_file_name;
if($ufile !=none)
 {
    if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
 {
 echo "The upload is successful<BR/>"; 
 echo "File Renamed to: ".$new_file_name." for processing.<BR/>"; 
 echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>"; 
 echo "<strong><a style='color:#6A8DBC; text-decoration:none' href='".$link_address."'>Proceed to the next phase of the importation of data to the system</a></strong></br>";
  }
 else
  {
   echo "Error";
   }
 }

  $zip = new ZipArchive;
  $res = $zip->open($new_file_name);
  if ($res === TRUE) {
     $zip->extractTo($random_digit.'/');
     $zip->close();
     echo 'extraction successful';
     } else {
     echo 'extraction error';
     }

Is it the mode of the folder which prevents extraction? As far as I can see, there is no syntax error.

是文件夹的模式阻止了提取吗?据我所知,没有语法错误。

采纳答案by DorianFM

try

尝试

$res = $zip->open($path)

As you have moved the file to $pathyou then need to operate on the file at $path

由于您已将文件移动到$path您,然后需要对文件进行操作$path

回答by Abdo-Host

<?php 

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.";
    }

  /* 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>