PHP Implementation Package Download File Method Example
- 2021-08-10 07:06:57
- OfStack
In this paper, an example is given to describe the method of packaging and downloading files by PHP. Share it for your reference, as follows:
/**
* Download a file
* @param $img
* @return string
*/
public function Download($img)
{
$items = [];
$names = [];
if($img)
{
// Used for front-end jump zip Link splicing
$path_redirect = '/zip/'.date('Ymd');
// Temporary file storage address
$path = '/tmp'.$path_redirect;
if(!is_dir($path))
{
mkdir($path, 0777,true);
}
foreach ($img as $key => $value) {
$fileContent = '';
$fileContent = $this->CurlDownload($value['url']);
if( $fileContent )
{
$__tmp = $this->SaveFile( $value['url'] , $path , $fileContent );
$items[] = $__tmp[0];
$names[] = $value['name'].'_'.($key+1).'.'.$__tmp[1];
}
}
if( $items )
{
$zip = new ZipArchive();
$filename = time().'download.zip';
$zipname = $path.'/'.$filename;
if (!file_exists($zipname)) {
$res = $zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($res) {
foreach ($items as $k => $v) {
$value = explode("/", $v);
$end = end($value);
$zip->addFile($v, $end);
$zip->renameName($end, $names[$k]);
}
$zip->close();
} else {
return '';
}
// Through the front end js Jump zip Address download , Let not use php Code download zip Documents
//if (file_exists($zipname)) {
// Spliced attachment address
//$redirect = Domain name .$path_redirect.'/'.$filename;
//return $redirect;
//header("Location:".$redirect);
//}
// Download to the client by writing files directly
if (file_exists($zipname)) {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename= Annex .zip'); // Filename
header("Content-Type: application/zip"); //zip Format
header("Content-Transfer-Encoding: binary"); // Tell the browser that this is 2 Binary document
header('Content-Length: ' . filesize($zipname)); // Tell the browser that the file size
@readfile($zipname);
}
// Delete temporary files
@unlink($zipname);
}
}
return '';
}
}
/**
* curl Get link content
* @param $url
* @return mixed|string
*/
public function CurlDownload($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$errno = curl_errno($ch);
$error = curl_error($ch);
$res=curl_exec($ch);
curl_close($ch);
if($errno>0){
return '';
}
return $res;
}
/**
* Save a temporary file
* @param $url
* @param $dir
* @param $content
* @return array
*/
public function SaveFile( $url ,$dir , $content)
{
$fname = basename($url); // Returns the filename portion of the path
$str_name = pathinfo($fname); // Returns the information of the file path in the form of an array
$extname = strtolower($str_name['extension']); // Convert extension to lowercase
$path = $dir.'/'.md5($url).$extname;
$fp = fopen( $path ,'w+' );
fwrite( $fp , $content );
fclose($fp);
return array( $path , $extname) ;
}
Quote:
$img = [['url'=>' Address url/1.jpg','name'=>' Name ']];
Download($img);
More readers interested in PHP can check the topic of this site: "PHP Operation zip File and Compression Skills Summary", "php File Operation Summary", "php Regular Expression Usage Summary", "PHP Operation and Operator Usage Summary", "PHP Basic Grammar Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php String (string) Usage Summary", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.