唯美句子
当前位置:首页 > 唯美句子 > 列表页

[dedecms实现自动打包文章中图片并下载的方法]文章id++dedecms

泥巴往事网  发布于:2018-08-15  分类: 唯美句子 手机版

本文实例讲述了dedecms实现自动打包文章中图片并下载的方法。分享给大家供大家参考。具体分析如下:

自己几年前的QQ图片网站所有的内容是直接复制上去了,这样我们现在提供了下载功能,但是当时并没有下载地址了,这样我们研究了一个可以自动当用户点击下载时再把当前文章中的图片利用ZipArchive压缩并实现下载,下面来看示例代码,代码如下:

代码如下:

include("data/common.inc.php"); //加载数据库

$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 " ";

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); //在这里创建压缩文件

echo " "; //创建好了再下载

exit;

}

else

{

echo "参数错误";

exit;

}

}

}

else

{

echo "参数错误";

exit;

}

//查询数据表

/*创建一个zip文件*/

function create_zip($files = array(),$destination = "",$overwrite = false) {

if(file_exists($destination) && !$overwrite){ //检测zip文件是否存在

return false;

}

if(is_array($files)) { //检测文件是否存在

foreach($files as $file) { //循环通过每个文件

if(file_exists($file)) { //确定这个文件存在

$valid_files[] = $file;

}

}

}

if(count($valid_files)) {

$zip = new ZipArchive(); //创建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数据库然后再进行根据文件ID查找数据并进行压缩了,打包好之后利用js输出就实现了下载,如果下次再下载这个文件就自动调用此文件而不再次打包查找数据库了,这样可以减少服务器负载.

希望本文所述对大家的dedecms建站有所帮助。

本文已影响

[dedecms实现自动打包文章中图片并下载的方法]文章id++dedecms