Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

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

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);
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...