plugins – can’t unzip file

Question

i’m created import and export function,
The Export function download zip file to my computer
The Import function when trys to extract zip file it’s fails and can’t open zip file.

when i create the zip file withiout php and upload it to import function, it works, but the file of export function is not.

here is my code:

Export

$zip = new ZipArchive;
          $zip_name="test.zip";
          if ($zip->open($zip_name, ZipArchive::CREATE) === TRUE)
          {
             
              $zip->addFile('1.csv', 'folder/1.csv');
        

              $zip->addFromString('folder/1.csv', 'first string to write');

              // All files are added, so close the zip file.
              $zip->close();

              header('Content-type: application/zip');
              header('Content-disposition: filename="' . $zip_name . '"');
              header("Content-length: " . filesize($zip_name));
              readfile($zip_name);
              exit();
          }
    

Import :

  function rmdir_recursive($dir) {
    foreach(scandir($dir) as $file) {
       if ('.' === $file || '..' === $file) continue;
       if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
       else unlink("$dir/$file");
   }

   rmdir($dir);
}

  if(isset($_POST))  
  {  

    if($_FILES["file"]["name"]) {
      $filename = $_FILES["file"]["name"];
      $source = $_FILES["file"]["tmp_name"];
      $type = $_FILES["file"]["type"];
      echo $type;
      $name = explode(".", $filename);
      $accepted_types = array('application/zip', 'application/x-zip-compressed', 
      'multipart/x-zip', 'application/x-compressed');
      foreach($accepted_types as $mime_type) {
          if($mime_type == $type) {
              $okay = true;
              break;
          } 
      }
  
      $continue = strtolower($name[1]) == 'zip' ? true : false;
      if(!$continue) {
          echo "The file you are trying to upload is not a .zip file. Please try again.";
      }
  
    /* PHP current path */
    $path = dirname(__FILE__).'/packages/';  // absolute path to the directory where zipper.php is in
    $filenoext = basename ($filename, '.zip');  // absolute path to the directory where zipper.php is in (lowercase)
    $filenoext = basename ($filenoext, '.ZIP');  // absolute path to the directory where zipper.php is in (when uppercase)
    
    $targetdir = $path . $filenoext; // target directory
    $targetzip = $path . $filename; // target zip file

    /* create directory if not exists', otherwise overwrite */
    /* target directory is same as filename without extension */
  
    if (is_dir($targetdir))  rmdir_recursive ( $targetdir);
  
  
    mkdir($targetdir, 0777);
    /* here it is really happening */
      
      if(move_uploaded_file($source, $targetzip)) {
          $zip = new ZipArchive;          // create object
          $res = $zip->open($targetzip);   // open archive
          if($res===TRUE) {
            $zip->extractTo($targetdir);        // extract contents to destination directory
            $zip->close();  
            echo 'extraced successed';      
          }else{
            echo 'extraced failed';
          }
          echo "Your .zip file was uploaded and unpacked.";
      } else {    
          echo "There was a problem with the upload. Please try again.";
      }
  }

}  

0
kovrita is The life 6 months 2022-06-15T17:27:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse