PHP 上传并解压 Zip

发布于 2024-11-09 14:15:00 字数 2022 浏览 0 评论 0原文

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

我在中间留下了一个很大的评论,我陷入了困境。不确定这是否与在 IIS 上运行有什么关系?

.zip 文件被上传到我期望的位置,并且我可以手动将其 ftp 回来并提取文件。我希望在这里提取文件,循环它们,如果它们是图像文件,然后将它们添加到图库图像的数据库中......不过,首先要做的事情。需要了解为什么我看不到 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.";
    }
}
?>

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.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-11-16 14:15:00

不是解决方案,而是解决方法:您可以控制机器吗?如果是这样,请安装 7-zip(在 Windows 上)或 unzip,使用 system('unzip ...')system('7z ...' ) 提取 zip 存档。

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.

北城挽邺 2024-11-16 14:15:00

ISP 安装了 zip 工作所需的组件,所以现在一切正常。感谢@timdream 提供替代方法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文