如何提取用户上传的 zip 文件?

how can I extract zip files, which was uploaded by user?

所以,我想创建一个系统,用户可以在 zip 文件中上传 3d 模型的文件,并且可以显示、存储模型等

所以,我得到了文件,我把它永久地放在一个文件夹中,然后将它解压缩到另一个临时文件夹中,只是为了看看它是否是 3d 模型。

我这样试过:

                  $target_dir = "upload/";
                  $targetfilename = rand().$_FILES['file']['name'];
                  move_uploaded_file($_FILES['file']['tmp_name'], $target_dir.$targetfilename);

                  //unzip the file into temp folder
                  $tmp_dir = $target_dir.rand();
                  mkdir($tmp_dir);  
                  chmod($tmp_dir, 0777);
                  //chmod($targetfilename, 0777); //this not working, maybe isn't the right way

                  $zip = new ZipArchive;
                  $res = $zip->open($targetfilename);
                  if ($res === TRUE) {
                    // extract it to the path we determined above
                    $zip->extractTo($tmp_dir);
                    $zip->close();
                    echo 'SUCCESS';
                  } else {
                    echo 'ERROR';
                  }

我没有收到任何错误,但无法解压缩 zip。任何的想法?我该如何解决?

$targetfilename 不是在 $target_dir 文件夹中吗?

如果是这样,改变 $res = $zip->open($targetfilename);

$res = $zip->open($target_dir.$targetfilename); 可能会解决您的问题。