• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Extracting Zip File

Hi there,

   Can you give me a code in PHP to extract the zip files and store that in a prementioned folder?

Thanks.
0
gstalin
Asked:
gstalin
  • 3
  • 2
1 Solution
 
BogoJokerCommented:
Hi gstalin,

I misread the questions and I moved the actual .zip folder.  Here is a nice approach to locate the .zip file though.  Next we just have to find out how to modify .zip files with php and move the inner files.

<?php
// Directory Name
$dirName = 'zips';
// New Directory Name (where to move zip files)
$newDirName = 'zips/zip2';

// Open Directory
$dir = opendir($dirName);

// Read Files
while ($file = readdir($dir))
{
  // Check if file ends in .zip
  if (preg_match('/\.zip$/i', $file))
  {
    // Move the file
    // On Unix you can do this easily by executing a terminal command
    // to move a file: rm oldFileName newLocationAndName

    // General case
    // Get Contents
    $contents = file_get_contents($dirName . '/' . $file);

    // Create a file with the same name
    $newFile = fopen($newDirName . '/' . $file, "x+");
    fwrite($newFile, $contents);
   
    // Close the file handler
    fclose($newFile);
   
    // Echo
    echo "Moved: $dirName/$file to $newDirName/$file";
  }
}

?>

Just set your own $dirName and $newDirName and this should work to move the .zips.  This does not delete the original file!  Just wanted to let you know that.  Therefore it is safe to test with.

Joe P
0
 
gstalinAuthor Commented:
Hi,

  I have tested your code, but it only moves the .zip extension files. But it not extrcting the Zip files!

Thanks
0
 
BogoJokerCommented:
gstalin, that is what I said it did.  I do not yet know how to extract files from inside a .zip.

Sorry,
Joe P
0
 
gstalinAuthor Commented:
Ok no problem, Thank you for your all efforts.
0
 
gstalinAuthor Commented:
thanks Joe,

I got the code for Extracting the Zip.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now