使用标头在 php 中下载 zip 文件 [英] download zip files in php using headers

查看:27
本文介绍了使用标头在 php 中下载 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用于下载 zip 文件的 PHP 脚本:

I have following PHP Script for downloading the zip files:

header("Content-length:".filesize($archive_file_name));
header('Content-Type: application/zip'); // ZIP file
header('Content-Disposition: attachment; filename="downloadpackage.zip"');
header('Content-Transfer-Encoding: binary');
readfile($archive_file_name);
exit();

但它不会强制下载 zip 文件,而是显示了许多不寻常的字符,如下所示:

But it doesn't force the download the zip file instead, it shows the lots of unusual characters like below:

PK[ FA # X$D:\voice_06101243342pm014771957.wav | s] y b CI\ {2m M' Eibu2\@B钋{ 6b# $H,\@ : Z (q ә Y q , d y Otp$PĽg w} } r֯[7 u "w |o 7/Z ˋ^Z Ң ZTZ\S|Nc X8 c X8 c X8 c X8 c X8 ۲, a oA*=ە do: |ˉ'rb |L2 *^ :q׵}/ \ s Z? G [ =ϗ?

PK[�FA����#�X$D:\voice_06101243342pm014771957.wav�|�s]�y���b�CI\��{2m��M'�Eibu2\@B��܋{� 6b# $H,\@��:��Z�(q�����ә���Y��q�����,� ���� d��y��O�tp$PĽg��w}��}�r֯[7�u�"w���|o�7/Z��ˋ^Z������Ң�ZTZ\S|N����c�X8��c�X8��c�X8��c�X8��c�X8���۲,��a�oA*=ە���do:��|ˉ'rb�|�L2�*^�:q׵}/��\�s�Z?��G�[��=ϗ?

我还在 headers 函数之前放置了以下代码:

I have also placed the below code before the headers function :

if (headers_sent()) 
{
  // HTTP header has already been sent
  return false;
}
// clean buffer(s)
while (ob_get_level() > 0)
{
   ob_end_clean();
}

忽略显示那些字符和 zip 也不会强制下载.我无法弄清楚问题,请帮助我,将不胜感激.

This ignore to display those character and zip is also not force to download. I couldn't figure out the problem, please help me, it will be greatly appreciated.

推荐答案

这里是我用来下载文件的简单函数:

Here is the simple function I use to download files:

function _download ($file_name) {
  $src = fopen($this->download_path . $file_name,'r') or die("\n\n ========= Could not download $file_name\n");
  $dest = fopen($file_name,'w');
  stream_copy_to_stream($src, $dest);
}

这篇关于使用标头在 php 中下载 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆