【问题标题】:PHP | Download the zip file in phpPHP |在php中下载zip文件
【发布时间】:2015-08-03 07:46:49
【问题描述】:

我想在 php.ini 中下载 zip 文件。 下面是我的代码。

<?php 
ob_start();
// set example variables
$filename = "test.zip";
$filepath = "/home/somewhere/file/zip";
// http headers for zip downloads
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
readfile($filepath.$filename);
?>

我的 html 代码中有一个链接。

<html>
    <head>
    </head>
    <body>
        <a href="myPHPfile.php">Download</a>
    </body>
</html>

当我单击名为“Donwload”的链接时,文件已成功下载,但我无法解压缩并打开文件。

第一次下载时文件名为test.zip,只是文件名。它的扩展是在我单击它解压缩时创建的。 文件的扩展名是“.cpgz”而不是“.zip”。

这是我下载时的日志。

Resource interpreted as Document but transferred with MIME type application/zip:"myPHPfile.php"

我的代码做错了吗?还是错过了什么? 我怎样才能解决这个问题? 我上传的文件已经压缩在服务器中,我要做的就是下载它。

【问题讨论】:

  • 嗨,看看这个post,可能会有所帮助。
  • ob_start(); 在那里做什么?

标签: php


【解决方案1】:

问题是您的文件路径,在“/home/somewhere/file/zip”之后缺少尾随“/”,工作目录应该是: $filepath = "/home/somewhere/file/zip/";

【讨论】:

  • 但他说文件下载成功
  • 我自己试过了,它可以正确使用斜杠。
【解决方案2】:

现在这段代码运行良好!

<?php
$filepath = iconv("UTF-8","CP949", "/home/somewhere/zip/test.zip");

header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=test.zip");
header("Content-Transfer-incoding: utf-8");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));

ob_end_flush();
readfile($filepath);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2013-11-02
    • 1970-01-01
    • 2016-11-12
    • 2017-10-20
    相关资源
    最近更新 更多