unzip.php File Reference

Go to the source code of this file.

Functions

  • unzip ($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
    Unzip the source_file in the destination dir.
  • create_dirs ($path)

Function Documentation

create_dirs ( path  ) 

Definition at line 95 of file unzip.php.

Referenced by unzip().

00096 {
00097   if (!is_dir($path))
00098   {
00099     $directory_path = "";
00100     $directories = explode("/",$path);
00101     array_pop($directories);
00102 
00103     foreach($directories as $directory)
00104     {
00105       $directory_path .= $directory."/";
00106       if (!@is_dir($directory_path))
00107       {
00108         @mkdir($directory_path);
00109         chmod($directory_path, 0777);
00110       }
00111     }
00112   }
00113 }

unzip ( src_file,
dest_dir = false,
create_zip_name_dir = true,
overwrite = true 
)

Unzip the source_file in the destination dir.

Parameters:
$src_file string The path to the ZIP-file.
$dest_dir string The path where the zipfile should be unpacked, if false the directory of the zip-file is used
$create_zip_name_dir boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
$create_zip_name_dir boolean Overwrite existing files (true) or not (false)
Returns:
boolean Succesful or not

Definition at line 13 of file unzip.php.

00014 {
00015   if(function_exists("zip_open"))
00016   {
00017       if(!is_resource(zip_open($src_file)))
00018       {
00019           $src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
00020       }
00021 
00022       if (is_resource($zip = zip_open($src_file)))
00023       {
00024           $splitter = ($create_zip_name_dir === true) ? "." : "/";
00025           if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
00026 
00027           // Create the directories to the destination dir if they don't already exist
00028           create_dirs($dest_dir);
00029 
00030           // For every file in the zip-packet
00031           while ($zip_entry = zip_read($zip))
00032           {
00033             // Now we're going to create the directories in the destination directories
00034 
00035             // If the file is not in the root dir
00036             $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
00037             if ($pos_last_slash !== false)
00038             {
00039               // Create the directory where the zip-entry should be saved (with a "/" at the end)
00040               create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
00041             }
00042 
00043             // Open the entry
00044             if (zip_entry_open($zip,$zip_entry,"r"))
00045             {
00046 
00047               // The name of the file to save on the disk
00048               $file_name = $dest_dir.zip_entry_name($zip_entry);
00049 
00050               // Check if the files should be overwritten or not
00051               if ($overwrite === true || $overwrite === false && !is_file($file_name))
00052               {
00053                 // Get the content of the zip entry
00054                 $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
00055 
00056                 if(!is_dir($file_name))
00057                 file_put_contents($file_name, $fstream );
00058                 // Set the rights
00059                 if(file_exists($file_name))
00060                 {
00061                     chmod($file_name, 0777);
00062                   //  echo "<span style=\"color:#1da319;\">file saved: </span>".$file_name."<br />";
00063                 }
00064                 else
00065                 {
00066                   //  echo "<span style=\"color:red;\">file not found: </span>".$file_name."<br />";
00067                 }
00068               }
00069 
00070               // Close the entry
00071               zip_entry_close($zip_entry);
00072             }
00073           }
00074           // Close the zip-file
00075           zip_close($zip);
00076       }
00077       else
00078       {
00079        // echo "No Zip Archive Found.";
00080         return false;
00081       }
00082 
00083       return true;
00084   }
00085   else
00086   {
00087       if(version_compare(phpversion(), "5.2.0", "<"))
00088       $infoVersion="(use PHP 5.2.0 or later)";
00089 
00090      // echo "You need to install/enable the php_zip.dll extension $infoVersion";
00091 
00092   }
00093 }