Domanda

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 :

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? How can I resolve it?

È stato utile?

Soluzione 3

The problem solved! Here's what I did : I change the code of creating zip file into this function from php.net user comments :

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.

Altri suggerimenti

PHP does not actually provide a function that extracts a ZIP including its directory structure. 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

instead of using:

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

use:

$path = $someDirectory. DIRECTORY_SEPARATOR .$someFile;

Change your code to this:

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

And it will work for both operating systems.

Good luck,

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top