PHP上传和解压缩 [英] PHP Upload and Extract Zip

查看:161
本文介绍了PHP上传和解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个脚本,该脚本允许上载.zip并提取其内容.我在网上获取了一个可以正常工作的示例代码,并在开始的b/c处添加了一个类,因为我的ISP的zip功能没有正确编译.

I'm trying to run a script that allows the upload of a .zip and extracts the contents. 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.

我在中间被卡住的地方留下了很大的评论.不知道这是否与在IIS上运行有关系吗?

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?

.zip文件已上传到我期望的位置,并且我可以手动将其ftp回并提取文件.我希望在此处提取文件,如果它们是图像文件,则将它们循环,然后将它们添加到图库图像数据库中……首先是第一件事.需要了解为什么我看不到zip的内容...

The .zip file gets uploaded where I'd expect it to, and I'm manually able to ftp it back and extract the files. 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...

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

推荐答案

ISP安装了zip正常工作所需的组件,因此一切都很好.感谢@timdream提供了一种替代方法.

ISP installed the necessary components for zip to work so all is well now. Thanks @timdream for an alternative approach.

这篇关于PHP上传和解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆