Php 将文件压缩到一个文件夹中,并将存档文件夹';姓名

Php 将文件压缩到一个文件夹中,并将存档文件夹';姓名,php,ziparchive,Php,Ziparchive,我对php非常陌生,我刚刚编写了我的第一个脚本,它工作得很好,但缺少最后一点。 该脚本将压缩包含php的文件夹中的所有文件,并创建一个可下载的归档文件。 这是密码 <?php $zipname = 'test.zip'; $zip = new ZipArchive; $zip->open('D://mypath//zip//$zipname', ZipArchive::CREATE); if ($dir_handle = opendir('./')) { while (fal

我对php非常陌生,我刚刚编写了我的第一个脚本,它工作得很好,但缺少最后一点。 该脚本将压缩包含php的文件夹中的所有文件,并创建一个可下载的归档文件。 这是密码

<?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");

?>

我想要实现的是拥有$zipname=“文件夹的名称.zip” 因此,如果php在“/mypath/blabla/”中,我希望我的zip$zipname为“blabla.zip”

任何帮助都将不胜感激

编辑: 以下是工作代码:

<?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);
?>
您可以使用getcwd():

它将返回当前文件夹的路径,然后您可以对结果进行散列,以仅获取文件夹的名称:

// 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';
这应该能奏效

如果还要使用父文件夹的名称,请执行以下操作:

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

您可以使用
readfile()
,而不是使用
header()重定向:


使用
basename()
只向用户显示最后一部分,而使用
readfile()
则给出实际文件,无论它在哪里。

并且您找不到获取路径的函数?readfile($zipname)让我下载一个具有正确名称的空存档。请注意,zip归档文件不是在我正在压缩的同一文件夹中创建的,它实际上位于/zip/文件夹中。我怎么说呢?没问题,别忘了把你的问题说成已经解决了:)事实上它还没有解决。我无法正确下载文件(下载的存档文件为空,但我可以看到服务器上正确创建了存档文件)。我想这是因为归档文件保存到了另一个目录,而我对标题不太在行……你的问题是用文件夹名命名zip文件,这就是我给你的代码的作用^^你仍然可以用更新的代码打开另一个问题,看看为什么你的文件下载不正确。如果我还想包括父文件夹的名称,该怎么办?
$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';
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
readfile($zipname);