Zip files in a folder and give the archive folder's name

淺唱寂寞╮ 提交于 2019-12-12 04:43:34

问题


I'm very new to php and I just made my first script, which is working fine, but is missing a final touch. The script zips all files in the folder containing the php and creates a downloadable archive. Here's the code

<?php

$zipname =  'test.zip';
$zip = new ZipArchive;
$zip->open('D://mypath//zip//$zipname', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
  while (false !== ($entry = readdir($dir_handle))) {
    if ($entry != "." && $entry != ".." && !strstr($entry,'.php') && !strstr($entry,'.zip')) {
      $zip->addFile($entry);
    }
  }
  closedir($dir_handle);
}
else {
  die('file not found');
}

$zip->close();

header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=$zipname");
header('Content-Length: ' . filesize($zipname));
header("Location: $zipname");

?>

What I'd like to achieve is having $zipname = "the name of the folder.zip" So, if the php is inside "/mypath/blablabla/" i want my zip $zipname to be "blablabla.zip"

Any help will be appreciated!

EDIT: here's the working code:

<?php
$zipname = getcwd();
$zipname = substr($zipname,strrpos($zipname,'\\')+1);
$zipname = $zipname.'.zip';
$zip = new ZipArchive;
$zip->open('D:/inetpub/webs/mydomaincom/zip/'.basename($zipname).'', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
  while (false !== ($entry = readdir($dir_handle))) {
if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
    $zip->addFile($entry);
}
}
closedir($dir_handle);
}
else {
die('file not found');
}

$zip->close();

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
header('Content-Length: ' . filesize($zipname));
header('Location: /zip/'.$zipname);
?>

回答1:


You could use getcwd() :

http://php.net/manual/fr/function.getcwd.php

$zipname = getcwd();

It'll return the path of the current folder, then you could just hash the result to get only the name of the folder :

// We remove all the uneeded part of the path
$zipname = substr($zipname,strrpos($zipname,'\\')+1);

//Then we add .zip to the result :
$zipname = $zipname.'.zip';

This should do the trick.

And if you want to also use the name of the parent folder :

$zipname = getcwd();

// We remove the uneeded part of the path
$parentFolderPath = substr($zipname, 0,strrpos($zipname,'\\'));
$parentFolder = substr($parentFolderPath, strrpos($parentFolderPath,'\\')+1);

//Keep current folder name
$currentFolder = substr($zipname,strrpos($zipname,'\\')+1);

//Join both
$zipname = $parentFolder.'_'.$currentFolder;

//Then we add .zip to the result :
$zipname = $zipname.'.zip';



回答2:


Instead of redirecting with header(), you could use readfile():

header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
readfile($zipname);

With basename() only the last part is shown to the user and with readfile() you are giving out the actual file, no matter where it is.



来源:https://stackoverflow.com/questions/33431590/zip-files-in-a-folder-and-give-the-archive-folders-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!