PHP package files are downloaded to local after zip packages

Source: Internet
Author: User
Tags ziparchive

This is a job need to package download the current product of all the pictures to the local, file format for the zip compressed package, packaging download files and pictures, the program details for the actual situation, the use of the need to write according to their own actual situation:

<?php
/********************************************* Program Description ********************************************
* Program function: Product picture package download, zip archive package format
************************************************************************************************/
Header ("Content-type:text/html;charset=utf-8");
if (!isset ($_get[' Pro_code ')) {
Exit (' Error, the page you visited does not exist! ');
}
$pro _code = $_get[' Pro_code ');
Get list
$dir = $_server[' Document_root '). ' /imgs/ft/'. substr ($pro _code,0,1). ' /'. substr ($pro _code,1,1). ' /‘;
$filelist = Getimglist ($dir);

$filepath = $_server[' Document_root '). ' /tmp/product_img/';
if (!is_dir ($filepath)) {
mkdir ($filepath);
}
File name and path
$filename = $filepath. $pro _code. Zip ';
$filename = Iconv ("Utf-8", "gb2312", $filename);//Solve Chinese cannot display the problem

if (!file_exists ($filename)) {
Regenerate files using the Ziparchive class
$zip = new Ziparchive ();
if ($zip->open ($filename, ziparchive::create)!== TRUE) {
Exit (' cannot open file, or file creation failed! ‘);
}
foreach ($filelist as $file) {
if (file_exists ($file) && iscurrentimg ($file, $pro _code)) {
$zip->addfile ($file, basename ($file));
}
}
$zip->close ();
}

Once created, you need to determine if the file exists again.
if (!file_exists ($filename)) {
Exit (' No files found! ‘);
}

The following is the output download;
Header ("cache-control:max-age=0");
Header ("Content-description:filetransfer");
Header (' Content-disposition:attachment;filename= '. basename ($filename));//File name
Header ("Content-type:application/zip");//zip format
Header ("Content-transfer-encoding:binary");//Tell the browser that this is a binary file
Header (' Content-length: '. FileSize ($filename));//Tell the browser, file size
@readfile ($filename);//output file;



Check whether the current product picture function
function iscurrentimg ($filepath, $pro _code) {
$file = substr ($filepath, Strrpos ($filepath, '/') +1,strrpos ($filepath, '-')-strrpos ($filepath, '/')-1);
if ($file = = $pro _code) {
return true;
}else{
return false;
}
}

Get Picture file list function
function Getimglist ($dir) {
$files = Array ();
if (Is_dir ($dir)) {
$file _dir = Scandir ($dir);
foreach ($file _dir as $file) {
if ($file = = '. ' | | $file = = ' ... ') {
Continue
}elseif (Is_dir ($dir. $file. /‘)){
$files = Array_merge ($files, Getimglist ($dir. $file. ' /‘));
}else{
Array_push ($files, $dir. $file);
}
}
}
return $files;
}

PHP package files are downloaded to local after zip packages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.