【问题标题】:zipping and downloading files from an HTML form array从 HTML 表单数组中压缩和下载文件
【发布时间】:2014-03-29 19:06:29
【问题描述】:

我正在尝试创建一个在线系统,通过该系统,我可以获取已在服务器上的文件夹中组织的文件(各种图片)并将它们压缩到单个文件夹中,然后下载。

到目前为止,看看stackoverflow,我想出了这个,当我使用链接的HTML进行测试时,下载的结果zip文件是一个带有zip扩展名(即user-date.zip)的文本文件包含一些错误:

<head>
<title>File Zipper</title>
</head>

<br />
<b>Warning</b>:  filesize(): stat failed for Files/adsfa-20140329.zip in <b>D:\xampp\htdocs\filezipper\ZipScript2.php</b> on line <b>33</b><br />
<br />
<b>Warning</b>:  readfile(Files/adsfa-20140329.zip): failed to open stream: No such file or directory in <b>D:\xampp\htdocs\filezipper\ZipScript2.php</b> on line <b>35</b><br />

这是我的 ZipScript2.php 代码:

<head>
<title>File Zipper</title>
</head>

<?php
$filelist = $_POST['filestozip'];
$username = $_POST['user'];
$today = date("Ymd");
$packagename = $username.'-'.$today.'.zip';
$package = new ZipArchive;

$package->open($packagename, ZipArchive::CREATE);

foreach ($filelist as $file) {
  $package->addFile('/Files/'.$file.'/pics', 'Files/pics'); 
}
$package->close();

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
    header('Content-Type: "application/octet-stream"');
    header('Content-Disposition: attachment; filename="'.basename('Files/'.$packagename).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Content-Transfer-Encoding: binary");
    header('Pragma: public');
    header("Content-Length: ".filesize('Files/'.$packagename));
} else {
    header('Content-Type: "application/octet-stream"');
    header('Content-Disposition: attachment; filename="'.basename('Files/'.$packagename).'"');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".filesize('Files/'.$packagename));
}
readfile('Files/'.packagename);
?>

在我的htdocs 文件夹中(我使用dreamweaver 创建它)我的php 文件与我的“文件”目录单独在一起,该目录将图片存放在它们各自的子目录中。注意:子目录是根据我的 HTML 表单传递的名称命名的,它们确实匹配(我已经通过插入各种 echo 命令进行检查)。

我不确定这是导致这种情况发生的原因 - 你们中的任何人都可以对这种情况有所了解吗?希望有修复? :)

【问题讨论】:

    标签: php html http-headers ziparchive


    【解决方案1】:

    你必须修复两个错误:

    • 第 21、26、29 和 35 行:在“文件”之前添加一个斜杠,就像使用 $package->addFile() 语句一样。

    • 删除开始“

    另请参阅:Creating and Downloading a file using PHP

    问候,

    -戈尔卡

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多