简体   繁体   English

PHP 上传并解压 Zip

[英]PHP Upload and Extract Zip

I'm trying to run a script that allows the upload of a.zip and extracts the contents.我正在尝试运行一个允许上传 a.zip 并提取内容的脚本。 I grabbed a sample code online that is supposed to work and added a class at the beginning b/c my ISP doesn't have the zip functionality compiled in correctly.我在网上获取了一个应该可以工作的示例代码,并在开头添加了一个 class,因为我的 ISP 没有正确编译 zip 功能。

I've left a big comment in the middle where I'm getting stuck.我在中间留下了一个很大的评论,我被卡住了。 Not sure if this has anything to do with it running on IIS?不确定这是否与在 IIS 上运行有关?

The.zip file gets uploaded where I'd expect it to, and I'm manually able to ftp it back and extract the files. .zip 文件上传到我期望的位置,我可以手动将其 ftp 恢复并提取文件。 I'm looking to extract the files here, loop over them, if they're image files, then add them to a database of gallery images... first things first though.我希望在这里提取文件,循环遍历它们,如果它们是图像文件,然后将它们添加到画廊图像数据库中......首先要做的事情。 Need to understand why I'm not seeing the contents of the zip...需要了解为什么我没有看到 zip 的内容...

<?php // need this bc of ISP settings
require($_SERVER['DOCUMENT_ROOT']."/_classes/ZipArchive.php");
?><?php
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.";
    }

    // I set up the _TEST dir with 777 permissions
    $target_path = $_SERVER['DOCUMENT_ROOT']."/_TEST/".$filename;
    if(move_uploaded_file($source, $target_path)) {
        $zip = new ZipArchive();
        $x = $zip->open($target_path);

        // **********************************************************
        // $x returns an error here
        // code: ER_OPEN
        // http://php.net/manual/en/function.ziparchive-open.php
        // Not sure why?????
        // **********************************************************

        if ($x === true) {
           $zip->extractTo($_SERVER['DOCUMENT_ROOT']."/_TEST/");
           $zip->close();
           unlink($target_path);
           $message = "Your .zip file was uploaded and unpacked.";
        }
        else {
           $message =  'failed';
        }
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}
?>

Not a solution but a workaround: do you have the control of the machine?不是解决方案,而是解决方法:您可以控制机器吗? If so, install 7-zip (on Windows) or unzip , use system('unzip...') or system('7z...') to extract the zip archive.如果是这样,请安装 7-zip(在 Windows 上)或unzip ,使用system('unzip...')system('7z...')来提取 zip 存档。

ISP installed the necessary components for zip to work so all is well now. ISP 安装了 zip 工作所需的组件,所以现在一切正常。 Thanks @timdream for an alternative approach.感谢@timdream 提供另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM