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.
gstalinAsked:
Who is Participating?
 
BogoJokerConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.