Page 1 of 1

SOLVED!! ZIP ARCHIVE CREATE after MKDIR and CHMOD Fails?

Posted: Tue Aug 30, 2011 8:11 am
by YiiWanAB
SOLVED!!!!!

For anyone having a problem with ziparchive its possible you have made the mistake when trying to apply it like I have made and I would like to point out something that might be completely overlooked... the use of opendir and what each value of $file (see below) implies...

$dir = opendir($newDir);
while(false !== ( $file = readdir($dir)) )
{ }

the following code above iterates over a dir and reads each file ""NAME"" only, not path and name, into $file variable. When you look at $file using a debugger or var_dump you will see the file name; however, that is the file name and not the file itself or a path. So when $zip->addFile($file) is called you are not looking in the directory created, you are looking somewhere else. One has to recreate the file path and name and pass that to addFile method of zip.

Hope this helps at least one other person out. I am posting the full index file for anyone who wants to see what I am referring to.

Code: Select all

<?php

   $newDir = getcwd() . DIRECTORY_SEPARATOR . "NewDir";
    
   mkdir($newDir);
    
    // add files
    
   $fileName1 = $newDir . DIRECTORY_SEPARATOR . "NewFile1.txt";
   $fileName2 = $newDir . DIRECTORY_SEPARATOR . "NewFile2.txt";
   
   file_put_contents($fileName1, "Here is some data");
   file_put_contents($fileName2, "Here is some more data");
      
   $newZipFile = "NewZipFile4.zip";
   
   
//    
//    
   $zip = new ZipArchive();
   
// 
   $zipFileName = $newDir . DIRECTORY_SEPARATOR . $newZipFile;
   if (file_exists($zipFileName))
   {
       $res = $zip->open($newDir . DIRECTORY_SEPARATOR . $newZipFile, ZIPARCHIVE::OVERWRITE);
   } else {
       $res = $zip->open($newDir . DIRECTORY_SEPARATOR . $newZipFile, ZIPARCHIVE::CREATE);
   }
   
    echo "Open Result: " . $res . "</br>";
    
    if($res === true)
    {
        $dir = opendir($newDir);
        
        while ($file = readdir($dir)) { 
            
         if (($file == "." || $file == "..")) { } else { 
             
             $fileDirAndName = $newDir . DIRECTORY_SEPARATOR . $file;
             If (file_exists($fileDirAndName)){
                 $f = file_get_contents($fileDirAndName);
                if (!$zip->addFromString($file, $f))
                {
                    print $file."was not added!<br />"; 
                }
                
             } else {
                 print $file . " does not exists </br>";
             }
             
             
         //We do not wanna this files in the new zip archive 
         // if (!$zip->addFile($newDir . DIRECTORY_SEPARATOR . $file)) { 
         //    print $file."was not added!<br />"; 
           } 
        } 
         
    }
    
    if (!$zip->close())
    {
       echo "Close Result: " . $zip->getStatusString() . "</br>";     
    } else {
        echo "Close Result: " . $res . "</br>";
    }
?>

....

....

Here is an update:

"Read error: No such file or directory" is returned on the close of the zip archive; however, the dir does indeed exist, so do the files created, but obviously not the zip.
Anyone??? This is really driving me nuts.

Another update:

If I just use $zip->addFromString("README.txt", "Files To Be Included"); and nothing else it gets created. w.t.f.

Thanks in advance.

I have a problem creating Zip Archives for newly created sub directories:

Ubuntu 11.04 with current lamp stack. Directory in /var/www/html/ft

I create a directory with mkdir, (I have also tried chmod on it as well)
I then adding files to it, which I can. No problem here.
I then wish to make a zip archive in the new directory and zip the files that I created; however, it fails.

I have used the same code in the current dir for testing and it works.

Notes: I exclude add "." and ".."

Permissions on /ft: drwxrwxr-x 4 www-data www-data 4096 2011-08-30 08:38 ft

Permissions on /ft/NewDir: drwxrwxrwx 2 www-data www-data 4096 2011-08-30 09:09 NewDir

Here is the code I used in a simple index file

Code: Select all

<?php

   $newDir = getcwd() . DIRECTORY_SEPARATOR . "NewDir";
    
    mkdir($newDir, 0777);
    chmod($newDir, 0777);
    // add files
    
    file_put_contents($newDir . DIRECTORY_SEPARATOR . "NewFile1.txt", "Here is some data");
    file_put_contents($newDir . DIRECTORY_SEPARATOR . "NewFile2.txt", "Here is some more data");
        
//    
    $newZipFile = "NewZipFile4.zip";
//    
   $zip = new ZipArchive();
//    
   $res = $zip->open($newDir . DIRECTORY_SEPARATOR . $newZipFile, ZIPARCHIVE::OVERWRITE);
    echo "Open Result: " . $res . "</br>";
//    
    if($res!=0)
    {
        $dir = opendir($newDir);
    
        while(false !== ( $file = readdir($dir))  ) 
        {
            
            $path_parts = pathinfo($file);
            
            if ( (!is_dir($file)) && ($file != ".") && ($file != "..") 
                    && ($path_parts['extension']=="txt"))
            {
                $zip->addFile($file);
            }
        }
        closedir($dir);
    }
    
    $res = $zip->close();
    echo "Close Result: " . $res . "</br>"; 
?>
Any help appreciated.

Here is the full working code: