Exemple #1
0
 function unzip($filename)
 {
     global $up_place, $path, $id_rep, $charset, $base_path;
     $zip = new zip($filename);
     $zip->readZip();
     $cpt = 0;
     if ($up_place && $path != '') {
         $up = new upload_folder($id_rep);
     }
     if (is_array($zip->entries) && count($zip->entries)) {
         foreach ($zip->entries as $file) {
             $encod = mb_detect_encoding($file['fileName'], "UTF-8,ISO-8859-1");
             if ($encod && $encod == 'UTF-8' && $charset == "iso-8859-1") {
                 $file['fileName'] = utf8_decode($file['fileName']);
             } elseif ($encod && $encod == 'ISO-8859-1' && $charset == "utf-8") {
                 $file['fileName'] = utf8_encode($file['fileName']);
             }
             if ($up_place && $path != '') {
                 $chemin = $path;
                 if ($up->isHashing()) {
                     $hashname = $up->hachage($file['fileName']);
                     @mkdir($hashname);
                     $filepath = $up->encoder_chaine($hashname . $file['fileName']);
                 } else {
                     $filepath = $up->encoder_chaine($up->formate_nom_to_path($chemin) . $file['fileName']);
                 }
                 //On regarde si le fichier existe avant de le créer
                 $continue = true;
                 $compte = 0;
                 $filepath_tmp = $filepath;
                 do {
                     if (!file_exists($filepath_tmp)) {
                         $continue = false;
                     } else {
                         $compte++;
                         if (preg_match("/^(.+)(\\..+)\$/i", $filepath, $matches)) {
                             $filepath_tmp = $matches[1] . "_" . $compte . $matches[2];
                         } else {
                             $filepath_tmp = $filepath . "_" . $compte;
                         }
                     }
                 } while ($continue);
                 if ($compte) {
                     $filepath = $filepath_tmp;
                 }
                 $fh = fopen($filepath, 'w+');
                 fwrite($fh, $zip->getFileContent($file['fileName']));
                 fclose($fh);
                 if ($compte) {
                     if (preg_match("/^(.+)(\\..+)\$/i", $file['fileName'], $matches)) {
                         $file['fileName'] = $matches[1] . "_" . $compte . $matches[2];
                     } else {
                         $file['fileName'] = $file['fileName'] . "_" . $compte;
                     }
                 }
             } else {
                 $chemin = $base_path . '/temp/' . $file['fileName'];
                 $fh = fopen($chemin, 'w');
                 fwrite($fh, $zip->getFileContent($file['fileName']));
                 $base = true;
             }
             $this->unzipped_files[$cpt]['chemin'] = $chemin;
             $this->unzipped_files[$cpt]['nom'] = $file['fileName'];
             $this->unzipped_files[$cpt]['base'] = $base;
             $cpt++;
         }
     }
 }