How to implement online package download function in PHP

墨辰丷
Release: 2023-03-28 19:48:01
Original
1871 people have browsed it

This article mainly introduces the implementation method of PHP online packaging and downloading function, and analyzes the implementation process and related precautions of PHP packaging and downloading in the form of examples. Friends in need can refer to it

Last night, in order to This matter of packaging and downloading has caused a lot of trouble. 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 Configuration Diagram

The second one tells you the path to the configuration document, just click to 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");
Copy after login

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHPStrom New FTP project and detailed graphic explanation of online operations

##php WeChat Pay’s public account payment function

PHP implements the function of uploading files through CURL

The above is the detailed content of How to implement online package download function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!