国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > 織夢DEDE > 正文

dedecms實現(xiàn)自動打包文章中圖片并下載

2024-07-12 08:43:34
字體:
供稿:網(wǎng)友

自己幾年前的QQ圖片網(wǎng)站所有的內(nèi)容是直接復(fù)制上去了,這樣我們現(xiàn)在提供了下載功能,但是當(dāng)時并沒有下載地址了,這樣我們研究了一個可以自動當(dāng)用戶點擊下載時再把當(dāng)前文章中的圖片利用ZipArchive壓縮并實現(xiàn)下載,下面來看.

示例代碼如下:

  1. include("data/common.inc.php"); //加載數(shù)據(jù)庫 
  2. $conn = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd) ;//or die(mysql_error()); 
  3. mysql_select_db($cfg_dbname,$conn); 
  4. mysql_query("set Names '$cfg_db_language'"); 
  5. $id = intval(isset($_GET['id'])?$_GET['id']:0); 
  6. if$id ) 
  7.  $zipUrl = 'uploads/zip/'.$id.'.zip'
  8.  iffile_exists($zipUrl) ) //判斷文件是否存在 
  9.  { 
  10.   echo '<script language="javascript">location.href="'.$zipUrl.'";</script>';   
  11.   exit
  12.  } 
  13.  else 
  14.  { 
  15.   $sql ="select url from ".$cfg_dbprefix."uploads where arcid=$id"
  16.   $query = mysql_query( $sql );// or die(mysql_error()); 
  17.   if( mysql_num_rows( $query ) ) 
  18.   { 
  19.     
  20.    $array = array(); 
  21.    while$rs = mysql_fetch_array( $query ) ) 
  22.    { 
  23.     $array[] = substr($rs['url'],1,strlen($rs['url'])-1); 
  24.    } 
  25.    //print_r($array); 
  26.    create_zip($array$zipUrl, true); //在這里創(chuàng)建壓縮文件 
  27.    echo '<script language="javascript">location.href="'.$zipUrl.'";</script>'//創(chuàng)建好了再下載  
  28.    exit
  29.   } 
  30.   else 
  31.   { 
  32.    echo '參數(shù)錯誤'
  33.    exit
  34.   } 
  35.  } 
  36. else 
  37.  echo '參數(shù)錯誤'
  38.  exit
  39. //查詢數(shù)據(jù)表 
  40.  
  41. /*創(chuàng)建一個zip文件*/ 
  42. function create_zip($files = array(),$destination = '',$overwrite = false) { 
  43.  if(file_exists($destination) && !$overwrite){ //檢測zip文件是否存在 
  44.   return false; 
  45.  } 
  46.  if(is_array($files)) { //檢測文件是否存在 
  47.   foreach($files as $file) { //循環(huán)通過每個文件 
  48.    if(file_exists($file)) { //確定這個文件存在 
  49.     $valid_files[] = $file
  50.    } 
  51.   } 
  52.  } 
  53.  if(count($valid_files)) { 
  54.   $zip = new ZipArchive(); //創(chuàng)建zip文件 
  55.   if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true){ 
  56.    return false; //Vevb.com 
  57.   } 
  58.   foreach($valid_files as $file) { //添加文件 
  59.    $zip->addFile($file,$file); 
  60.   } 
  61.   $zip->close(); 
  62.   return file_exists($destination); 
  63.  } else { 
  64.   return false; 
  65.  } 

前一段代碼是連接dedecms數(shù)據(jù)庫然后再進(jìn)行根據(jù)文件ID查找數(shù)據(jù)并進(jìn)行壓縮了,打包好之后利用js輸出就實現(xiàn)了下載,如果下次再下載這個文件就自動調(diào)用此文件而不再次打包查找數(shù)據(jù)庫了,這樣可以減少服務(wù)器負(fù)載哦.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵阳市| 安宁市| 荔波县| 南城县| 丹江口市| 微博| 昭苏县| 富平县| 体育| 板桥市| 集安市| 浦江县| 仙居县| 靖江市| 宝山区| 鄯善县| 招远市| 攀枝花市| 石渠县| 新巴尔虎右旗| 巴马| 苏尼特左旗| 鄂伦春自治旗| 南部县| 收藏| 隆尧县| 莱西市| 大同县| 兴化市| 民勤县| 天全县| 哈密市| 绍兴市| 乌兰县| 梁山县| 郸城县| 西畴县| 望谟县| 苏州市| 庆安县| 芜湖县|