Home>

I want to create and download a ZIP file

$file_path = $_SERVER ['DOCUMENT_ROOT']. "/ data /";
$zip = new ZipArchive ();
$zip_name = date ("Y-m-d"). ". zip";
$result = $zip->open ($file_path. $zip_name, ZIPARCHIVE :: CREATE | ZIPARCHIVE :: OVERWRITE);
if (! $result) {
  return;
}
// $re = array (0 =>"sample.docx", 1 =>"test.jpg");
foreach ($re as $v) {
  $zip->addFile ($file_path. $v);
}
$zip->close ();


As a result, the ZIP file is created, but a ZIP file is created that inherits the directory of $file_path.
When I extract a ZIP file, I want to create a ZIP file that does not include a directory.

What I did
$file_path = $_SERVER ['DOCUMENT_ROOT']. "/ data /";
$zip = new ZipArchive ();
$zip_name = date ("Y-m-d"). ". zip";
$result = $zip->open ($file_path. $zip_name, ZIPARCHIVE :: CREATE | ZIPARCHIVE :: OVERWRITE);
if (! $result) {
  return;
}
// $re = array (0 =>"sample.docx", 1 =>"test.jpg");
foreach ($re as $v) {
  $zip->addFile ("./".$v);
}
$zip->close ();


As a result, the ZIP file was not created

php