简体   繁体   English

用PHP提取主机上的zip文件会破坏目录结构

[英]Extracting zip file on host by PHP destroys directory structure

I have a directory structure like this : 我有这样的目录结构:

members/
  login.php
  register.php

I zip them by PHP ZipArchive in my windows machine, but when I upload it to linux host and extract there by PHP it gives me these as two files with no directory : 我在Windows机器中通过PHP ZipArchive将它们压缩,但是当我将其上传到linux主机并通过PHP提取到那里时,这两个文件都没有目录:

members\login.php
members\register.php

I want to have the complete directory structure on the host after unzipping the file. 解压缩文件后,我想在主机上拥有完整的目录结构。 Note that this unpacking code runs without any problem in my local machine. 请注意,此解压缩代码在我的本地计算机上运行没有任何问题。 Is it something about windows and linux or what? 与Windows和Linux有关吗? How can I resolve it? 我该如何解决?

PHP does not actually provide a function that extracts a ZIP including its directory structure. PHP实际上并未提供提取包含其目录结构的ZIP的功能。 I found the following code in a user comment in the manual: 我在手册的用户注释中找到了以下代码:

function unzip($zipfile)
{
    $zip = zip_open($zipfile);
    while ($zip_entry = zip_read($zip))    {
        zip_entry_open($zip, $zip_entry);
        if (substr(zip_entry_name($zip_entry), -1) == '/') {
            $zdir = substr(zip_entry_name($zip_entry), 0, -1);
            if (file_exists($zdir)) {
                trigger_error('Directory "<b>' . $zdir . '</b>" exists', E_USER_ERROR);
                return false;
            }
            mkdir($zdir);
        }
        else {
            $name = zip_entry_name($zip_entry);
            if (file_exists($name)) {
                trigger_error('File "<b>' . $name . '</b>" exists', E_USER_ERROR);
                return false;
            }
            $fopen = fopen($name, "w");
            fwrite($fopen, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)), zip_entry_filesize($zip_entry));
        }
        zip_entry_close($zip_entry);
    }
    zip_close($zip);
    return true;
}

Source here . 来源在这里

try DIRECTORY_SEPARATOR 尝试DIRECTORY_SEPARATOR

instead of using: 而不是使用:

$path = $someDirectory.'/'.$someFile; $ path = $ someDirectory。'/'。$ someFile;

use: 采用:

$path = $someDirectory. $ path = $ someDirectory。 DIRECTORY_SEPARATOR .$someFile; DIRECTORY_SEPARATOR。$ someFile;

Change your code to this: 将代码更改为此:

$zip = new ZipArchive; $ zip =新的ZipArchive;
if ($zip->open("module. DIRECTORY_SEPARATOR .$file[name]") === TRUE) { 如果($ zip-> open(“ module。DIRECTORY_SEPARATOR。$ file [name]”)=== TRUE){
$zip->extractTo('module. DIRECTORY_SEPARATOR'); $ zip-> extractTo('module。DIRECTORY_SEPARATOR');
} }

And it will work for both operating systems. 它将适用于两种操作系统。

Good luck, 祝好运,

The problem solved! 问题解决了! Here's what I did : I change the code of creating zip file into this function from php.net user comments : 这是我所做的:我将创建zip文件的代码从php.net用户注释更改为该函数:

function addFolderToZip($dir, $zipArchive){
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            //Add the directory
            $zipArchive->addEmptyDir($dir);
            // Loop through all the files
            while (($file = readdir($dh)) !== false) {
                //If it's a folder, run the function again!
                if(!is_file($dir . $file)){
                    // Skip parent and root directories
                    if(($file !== ".") && ($file !== "..")){
                        addFolderToZip($dir . $file . "/", $zipArchive);
                    }
                }else{
                    // Add the files
                    $zipArchive->addFile($dir . $file);
                }
            }
        }
    }
}
$zip = new ZipArchive;
$zip->open("$modName.zip", ZipArchive::CREATE);
addFolderToZip("$modName/", $zip);
$zip->close();

And in the host I wrote just this code to extract the zipped file : 在主机中,我只编写了以下代码以提取压缩文件:

copy($file["tmp_name"], "module/$file[name]");
$zip = new ZipArchive;
if ($zip->open("module/$file[name]") === TRUE) {
    $zip->extractTo('module/');
}
$zip->close();

It created the folder and sub-folders. 它创建了文件夹和子文件夹。 The only bug left is that it extracts every file in all subfolders in the main folder too, so there is two versions of each file in subfolders. 剩下的唯一错误是,它也提取了主文件夹中所有子文件夹中的每个文件,因此子文件夹中的每个文件都有两个版本。

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

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