Home  >  Article  >  Backend Development  >  PHP online package download function example

PHP online package download function example

高洛峰
高洛峰Original
2016-12-20 15:22:321242browse

The example in this article describes how to implement the PHP online package download function. I would like to share it with you for your reference. The details are as follows:

Last night, I was very busy doing this packaging and downloading thing. There are several problems, the first one is the problem of starting PHP_ZIP.dll. In other words, just remove the ";" sign in front of extend_dir in PHP.INI. Just restart IIS or apache.

The problem is that the real PHP.INI configured in the system cannot be found.

PHP online package download function example

PHP configuration diagram

The second one tells you the path to the configuration document, just click and find it. My head was spinning and I wasted a lot of time.

After opening the PHP_ZIP extension library, you can start packaging and downloading.

$filename= date("YmdHis");
$zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
$zip->open($filename.".zip", ZipArchive::OVERWRITE); //$data 就是从数据库里面读出的数字
foreach( $data as $val){
   $attachfile = "E:\bysj\bysj".$val['fileaddr'].$val['filename']; //写硬路径,但建议用PHP环境变量
   $attachfile=iconv("UTF-8","GBK",$attachfile); //转码,是打包中文文档的关键
   $zip->addFile( $attachfile , basename($attachfile)); //压栈
}
$zip->close();//关闭
header("Content-type:text/html;charset=utf-8");//设置页面编号
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小
@readfile($filename.".zip");

The above code can basically realize document packaging and downloading, and also solves the problem that Chinese documents cannot be packaged and downloaded.

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP online package download function examples, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn