2

我有一个这样的目录结构:

members/
  login.php
  register.php

我在我的 Windows 机器上通过 PHP ZipArchive 压缩它们,但是当我将它上传到 linux 主机并通过 PHP 解压缩时,它给了我两个没有目录的文件:

members\login.php
members\register.php

解压文件后,我想在主机上拥有完整的目录结构。请注意,此解包代码在我的本地计算机上运行没有任何问题。是关于windows和linux的还是什么?我该如何解决?

4

3 回答 3

0

PHP 实际上并没有提供提取 ZIP 及其目录结构的函数。我在手册的用户评论中找到了以下代码:

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;
}

来源在这里

于 2013-08-20T13:46:15.873 回答
0

尝试DIRECTORY_SEPARATOR

而不是使用:

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

采用:

$path = $someDirectory. DIRECTORY_SEPARATOR .$someFile;

将您的代码更改为:

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

它适用于两种操作系统。

祝你好运,

于 2013-08-20T13:51:19.830 回答
0

问题解决了!这是我所做的:我将创建 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();

在主机中,我只写了这段代码来提取压缩文件:

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

它创建了文件夹和子文件夹。剩下的唯一错误是它也提取了主文件夹中所有子文件夹中的每个文件,因此子文件夹中的每个文件都有两个版本。

于 2013-08-21T14:51:23.277 回答