Thursday, June 16, 2022
HomeWordPress Developmentplugins - cannot unzip file

plugins – cannot unzip file


i am created import and export perform,
The Export perform obtain zip file to my laptop
The Import perform when trys to extract zip file it is fails and might’t open zip file.

when i create the zip file withiout php and add it to import perform, it really works, however the file of export perform is just not.

right here is my code:

Export

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

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

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

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

Import :

  perform rmdir_recursive($dir) {
    foreach(scandir($dir) as $file)  '..' === $file) proceed;
       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"];
      $supply = $_FILES["file"]["tmp_name"];
      $sort = $_FILES["file"]["type"];
      echo $sort;
      $title = explode(".", $filename);
      $accepted_types = array('software/zip', 'software/x-zip-compressed', 
      'multipart/x-zip', 'software/x-compressed');
      foreach($accepted_types as $mime_type) {
          if($mime_type == $sort) {
              $okay = true;
              break;
          } 
      }
  
      $proceed = strtolower($title[1]) == 'zip' ? true : false;
      if(!$proceed) {
          echo "The file you are attempting to add is just not a .zip file. Please strive once more.";
      }
  
    /* PHP present path */
    $path = dirname(__FILE__).'/packages/';  // absolute path to the listing the place zipper.php is in
    $filenoext = basename ($filename, '.zip');  // absolute path to the listing the place zipper.php is in (lowercase)
    $filenoext = basename ($filenoext, '.ZIP');  // absolute path to the listing the place zipper.php is in (when uppercase)
    
    $targetdir = $path . $filenoext; // goal listing
    $targetzip = $path . $filename; // goal zip file

    /* create listing if not exists', in any other case overwrite */
    /* goal listing is similar as filename with out extension */
  
    if (is_dir($targetdir))  rmdir_recursive ( $targetdir);
  
  
    mkdir($targetdir, 0777);
    /* right here it's actually occurring */
      
      if(move_uploaded_file($supply, $targetzip)) {
          $zip = new ZipArchive;          // create object
          $res = $zip->open($targetzip);   // open archive
          if($res===TRUE) {
            $zip->extractTo($targetdir);        // extract contents to vacation spot listing
            $zip->shut();  
            echo 'extraced successed';      
          }else{
            echo 'extraced failed';
          }
          echo "Your .zip file was uploaded and unpacked.";
      } else {    
          echo "There was an issue with the add. Please strive once more.";
      }
  }

}  

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments