View Single Post
  #1   IP: 153.99.20.153
Old 2014-07-18, 11:15 AM
Alfredo Alfredo is offline
初级会员
 
Join Date: 2009-05-24
Posts: 3
Alfredo 现在声名狼藉
Default Unzip a file with php

the best way to extract the zip file into the same directory in which it resides is to determine the hard path to the file and extract it specifically to that location. So, you could do:

Code:
// assuming file.zip is in the same directory as the executing script.
$file = 'file.zip';

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
  // extract it to the path we determined above
  $zip->extractTo($path);
  $zip->close();
  echo "WOOT! $file extracted to $path";
} else {
  echo "Doh! I couldn't open $file";
}
Reply With Quote