亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        dedecms實(shí)現(xiàn)自動(dòng)打包文章中圖片并下載的方法

        字號(hào):


            本文實(shí)例講述了dedecms實(shí)現(xiàn)自動(dòng)打包文章中圖片并下載的方法。分享給大家供大家參考。具體分析如下:
            自己幾年前的QQ圖片網(wǎng)站所有的內(nèi)容是直接復(fù)制上去了,這樣我們現(xiàn)在提供了下載功能,但是當(dāng)時(shí)并沒(méi)有下載地址了,這樣我們研究了一個(gè)可以自動(dòng)當(dāng)用戶(hù)點(diǎn)擊下載時(shí)再把當(dāng)前文章中的圖片利用ZipArchive壓縮并實(shí)現(xiàn)下載,下面來(lái)看示例代碼,代碼如下:
            復(fù)制代碼代碼如下:include("data/common.inc.php"); //加載數(shù)據(jù)庫(kù)
            $conn = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd) ;//or die(mysql_error());
            mysql_select_db($cfg_dbname,$conn);
            mysql_query("set Names '$cfg_db_language'");
            $id = intval(isset($_GET['id'])?$_GET['id']:0);
            if( $id )
            {
            $zipUrl = 'uploads/zip/'.$id.'.zip';
            if( file_exists($zipUrl) ) //判斷文件是否存在
            {
            echo '<script language="javascript">location.href="'.$zipUrl.'";</script>';
            exit;
            }
            else
            {
            $sql ="select url from ".$cfg_dbprefix."uploads where arcid=$id";
            $query = mysql_query( $sql );// or die(mysql_error());
            if( mysql_num_rows( $query ) )
            {
            $array = array();
            while( $rs = mysql_fetch_array( $query ) )
            {
            $array[] = substr($rs['url'],1,strlen($rs['url'])-1);
            }
            //print_r($array);
            create_zip($array, $zipUrl, true); //在這里創(chuàng)建壓縮文件
            echo '<script language="javascript">location.href="'.$zipUrl.'";</script>'; //創(chuàng)建好了再下載
            exit;
            }
            else
            {
            echo '參數(shù)錯(cuò)誤';
            exit;
            }
            }
            }
            else
            {
            echo '參數(shù)錯(cuò)誤';
            exit;
            }
            //查詢(xún)數(shù)據(jù)表 </p> <p>/*創(chuàng)建一個(gè)zip文件*/
            function create_zip($files = array(),$destination = '',$overwrite = false) {
            if(file_exists($destination) && !$overwrite){ //檢測(cè)zip文件是否存在
            return false;
            }
            if(is_array($files)) { //檢測(cè)文件是否存在
            foreach($files as $file) { //循環(huán)通過(guò)每個(gè)文件
            if(file_exists($file)) { //確定這個(gè)文件存在
            $valid_files[] = $file;
            }
            }
            }
            if(count($valid_files)) {
            $zip = new ZipArchive(); //創(chuàng)建zip文件
            if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true){
            return false;
            }
            foreach($valid_files as $file) { //添加文件
            $zip->addFile($file,$file);
            }
            $zip->close();
            return file_exists($destination);
            } else {
            return false;
            }
            }
            前一段代碼是連接dedecms數(shù)據(jù)庫(kù)然后再進(jìn)行根據(jù)文件ID查找數(shù)據(jù)并進(jìn)行壓縮了,打包好之后利用js輸出就實(shí)現(xiàn)了下載,如果下次再下載這個(gè)文件就自動(dòng)調(diào)用此文件而不再次打包查找數(shù)據(jù)庫(kù)了,這樣可以減少服務(wù)器負(fù)載.
            希望本文所述對(duì)大家的dedecms建站有所幫助。