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