1、先找到dedecms圖片集模型的templets生成圖片集的html代碼(album_add.htm)
<tr>
<td height="24" bgcolor="#F9FCEF" class="bline2"> <strong>圖集選項:</strong></td>
</tr>
<tr>
<td height="24" colspan="4" class="bline" style="padding-top:6px">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80"> <b>表現方式:</b></td>
<td>
<input name="pagestyle" class="np" type="radio" id="pagestyle1" onClick="checkMuList()" value="1"<?php if($cfg_album_style==1) echo " checked='checked'"; ?> />
<img src='images/alb2.gif' alt='單頁多圖樣式' title='單頁多圖樣式' />
<input name="pagestyle" class="np" type="radio" id="pagestyle2" onClick="checkMuList()" value="2"<?php if($cfg_album_style==2) echo " checked='checked'"; ?> />
<img src='images/alb1.gif' alt='幻燈片樣式' title='幻燈片樣式' />
<input name="pagestyle" class="np" type="radio" id="pagestyle3" onClick="checkMuList()" value="3"<?php if($cfg_album_style==3) echo " checked='checked'"; ?>/>
<img src='images/alb3.gif' alt='多縮略圖樣式' title='多縮略圖樣式' />
</td>
</tr>
</table></td>
</tr>
<tr id='cfgmulist' style='display:none'>
<td height="24" colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="26"> 多列式參數:</td>
<td>行
<input name="row" type="text" id="row" value="<?php echo $cfg_album_row; ?>" size="6">
列
<input name="col" type="text" id="col" value="<?php echo $cfg_album_col; ?>" size="6">
縮略圖寬度限制:
<input name="ddmaxwidth" type="text" id="ddmaxwidth" value="<?php echo $cfg_album_ddwidth; ?>" size="6">
像素</td>
</tr>
<tr>
<td width="80" height="26"> </td>
<td><strong>(系統僅是每頁輸出"行x列"張圖片,實現需自行控制樣式表)</strong></td>
</tr>
</table></td>
</tr>
<tr id='spagelist' style='display:none'>
<td height="24" colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="95"> 每頁圖片數:</td>
<td>
<input name='pagepicnum' type='text' id='pagpicnum' size='10' value='<?php echo $cfg_album_pagesize; ?>' />
單頁多圖顯示需要設置此參數,這種模式下系統不會為每張圖單獨生成縮略圖。
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="24" colspan="4" class="bline"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90"> <b>上傳方式:</b></td>
<td>
<input name="isrm" class="np" type="hidden" id="isrm" value="1" />
<input name="byhand" class="np" type="checkbox" id="byhand" value="1" disabled="ture" checked />
手工上傳
<input name="formzip" class="np" type="checkbox" id="formzip" value="1" onClick="showZipField(this,'zipff','htmlfield');" />
從ZIP壓縮包中解壓圖片
<input name="formhtml" class="np" type="checkbox" id="formhtml" value="1" onClick="showHtmlField(this,'htmlfield','zipff');" />
網上復制圖片
</td>
</tr>
</table></td>
</tr>
<tr id="zipff" style="display:none">
<td colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0" height="100">
<tr>
<td width="90"> 壓縮包文件:</td>
<td><input type='text' name='zipfile' id='zipfile' style='width:300px'>
<input name='addon_bt' type='button' class='inputbut' value='瀏覽...' onClick="SelectSoft('form1.zipfile')">
<input name="delzip" type="checkbox" id="delzip" value="1" checked="checked" />
處理后刪除壓縮文件 </td>
</tr>
</table></td>
</tr>
<tr id="htmlfield" style="display:none">
<td height="24" colspan="4" class="bline"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90"> 網上復制:</td>
<td> 復制的源網址:
<input name='copysource' type='text' value='http://' style='width:250px'/>
(防盜鏈網站需填寫您復制圖片的其中一個網頁網址) <br />
把圖片從別的網頁復制,在這里用Ctrl+V粘貼,提交后程序可直接獲取這個區域的所有圖片
<input type="button" name="button" id="button" value="預覽" onClick="LoadTestDiv()">
<br />
<div id='copyhtml' style='margin-top:6px;margin-bottom:6px;border:solid 1px #CCCCCC;width:96%;padding:6px;height:250px;width:100%' contenteditable="true">粘貼到這里...</div> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="100%" height="24" colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80" height="40"> <b>手工上傳:</b></td>
<td>
<div style="display: inline; border: solid 1px #7FAAFF; padding: 2px;">
<span id="spanButtonPlaceholder"></span>
</div>
</td>
</tr>
</table></td>
</tr>
<tr id="handfield">
<td colspan="4" class="bline" style="background:url(images/albviewbg.gif) #fff 0 20px no-repeat;">
<table width='100%' height='160' style="margin:0 0 20px 0">
<tr>
<td>
<div id="divFileProgressContainer" style="height:75px;"></div>
<div id="thumbnails"></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="24" colspan="4">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90"> 圖集內容:</td>
<td> <?php GetEditor("body","",450,"Diy"); ?></td>
</tr>
</table>
</td>
</tr>
將html代碼放入自定義模型關聯文檔templets,然后一個一個解決關聯后台數據庫的問題
上傳方式選擇效果失效(js問題無疑)
查看了下調試器報錯
showZipField is not defined
解決辦法:自然就是找到相應js文件並且引入->album.js
手工上傳行顯示不正常(應該是讀取后台信息出錯)
查看源代碼,發現該處引用了一個swfUpload的object,推測是先找到執行函數然后引入swfUpload的js文件
執行函數在album_add.htm處找到
window.onload = function () { swfu = new SWFUpload( { // Backend Settings upload_url: "swfupload.php", post_params: {"PHPSESSID": "<?php echo session_id(); ?>", "dopost" : ""}, // File Upload Settings file_size_limit : "2 MB", // 2MB file_types : "*.jpg; *.gif; *.png", file_types_description : "選擇 JPEG/GIF/PNG 格式圖片", file_upload_limit : "0", file_queue_error_handler : fileQueueError, file_dialog_complete_handler : fileDialogComplete, upload_progress_handler : uploadProgress, upload_error_handler : uploadError, upload_success_handler : uploadSuccess, upload_complete_handler : uploadComplete, button_image_url : "../images/SmallSpyGlassWithTransperancy_17x18.png", button_placeholder_id : "spanButtonPlaceholder", button_width: 250, button_height: 18, button_text : '<span class="button">選擇本地圖片 <span class="buttonSmall">(單圖最大為 2 MB,支持多選)</span></span>', button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }', button_text_top_padding: 0, button_text_left_padding: 18, button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT, button_cursor: SWFUpload.CURSOR.HAND, // Flash Settings flash_url : "../images/swfupload/swfupload.swf", custom_settings : { upload_target : "divFileProgressContainer" }, // Debug Settings debug: false }); };
將執行函數和庫文件引入之后報錯
fileQueueError is not defined
繼續找在handle.js里面有定義 直接引入該文件
引入該文件后,內容顯示出來了但是樣式不正常,控制台里面沒有報錯
查看樣式來源swfUpload.css文件,直接引用,顯示正常,問題解決,繼續找下一個問題。
好像前端顯示與圖片集基本一致,沒有什么問題,那就進入后台邏輯修改。
2、完善圖片集上傳邏輯
后台上傳主要涉及到
-
- 文件上傳,保存在什么地方
- 數據庫保存,寫入在什么表什么字段
首先分析圖片集模型的上傳流程
我本機圖片集上傳就有問題。。。。彈出了302和Error #2038alert框,先解決該問題吧
首先找alert框出處,在代碼中沒找到,因為是swfUpload flash插件,所以直接百度了,第一個推測原因是因為沒有帶上sessionID造成的,直接在post參數里面帶上sessionID(找到上傳成功時候參數形式)
post_params: {"PHPSESSID": "<?php echo session_id(); ?>", "dopost" : ""},
在參數設置里面直接找到了這個參數..說明不是這個問題,找其他原因吧
https://www.genban.org/news/dedecms-7582.html這里出現一種解決辦法,增加http GET獲取session的方式
if (isset($_POST["PHPSESSID"])) { session_id($_POST["PHPSESSID"]); } else if(isset($_GET["PHPSESSID"])) { session_id($_GET["PHPSESSID"]); }
這里先留個坑吧。。。。。
先回到圖片集模型的上傳流程吧
兩個問題:1、圖片保存在uploads 文件夾 allimg里面
2、數據保存在dede_addimg數據表里面
先解決圖片上傳問題(上傳直接沒問題),那就剩數據保存問題了。

先給自定義模型添加圖集相關字段pagestyle,maxwidth,imgurls,row,col,isrm,ddmaxwidth,pagepicnum這里也涉及一個問題,怎么添加這些字段呢?如果在模型字段里面手動添加的話,添加文章頁面就會渲染這些字段,當然這不是我想要的,所以 先把不需要出現的屬性直接暴力添加到數據表里吧
添加好數據表字段之后,就是獲取和插入數據了
還是先分析原邏輯,找到這些字段的獲取處理和插入操作
<?php /** * 文檔發布 * * @version $Id: archives_add.php 1 8:26 2010年7月12日Z tianya $ * @package DedeCMS.Administrator * @copyright Copyright (c) 2007 - 2010, DesDev, Inc. * @license http://help.dedecms.com/usersguide/license.html * @link http://www.dedecms.com */ require_once(dirname(__FILE__).'/config.php'); CheckPurview('a_New,a_AccNew'); require_once(DEDEINC.'/customfields.func.php'); require_once(DEDEADMIN.'/inc/inc_archives_functions.php'); if(empty($dopost)) $dopost = ''; if($dopost != 'save') { require_once(DEDEINC.'/dedetag.class.php'); require_once(DEDEADMIN.'/inc/inc_catalog_options.php'); ClearMyAddon(); $channelid = empty($channelid) ? 0 : intval($channelid); $cid = empty($cid) ? 0 : intval($cid); //獲得頻道模型ID if($cid > 0 && $channelid == 0) { $row = $dsql->GetOne("SELECT channeltype FROM `#@__arctype` WHERE id='$cid'; "); $channelid = $row['channeltype']; } else { if($channelid==0) { ShowMsg('無法識別模型信息,因此無法操作!', '-1'); exit(); } } //獲得頻道模型信息 $cInfos = $dsql->GetOne(" SELECT * FROM `#@__channeltype` WHERE id='$channelid' "); $channelid = $cInfos['id']; //獲取文章最大id以確定當前權重 $maxWright = $dsql->GetOne("SELECT COUNT(*) AS cc FROM #@__archives"); include DedeInclude('templets/archives_add.htm'); exit(); } /*-------------------------------- function __save(){ } -------------------------------*/ else if($dopost=='save') { require_once(DEDEINC.'/image.func.php'); require_once(DEDEINC.'/oxwindow.class.php'); $flag = isset($flags) ? join(',',$flags) : ''; $notpost = isset($notpost) && $notpost == 1 ? 1: 0; if(empty($click)) $click = ($cfg_arc_click=='-1' ? mt_rand(50, 200) : $cfg_arc_click); if(empty($typeid2)) $typeid2 = 0; if(!isset($autokey)) $autokey = 0; if(!isset($remote)) $remote = 0; if(!isset($dellink)) $dellink = 0; if(!isset($autolitpic)) $autolitpic = 0; // 圖集相關--------------------- if(!isset($formhtml)) $formhtml = 0; if(!isset($formzip)) $formzip = 0; if(!isset($ddisfirst)) $ddisfirst = 0; if(!isset($delzip)) $delzip = 0; // 圖集相關--------------------- if(empty($click)) $click = ($cfg_arc_click=='-1' ? mt_rand(50, 200) : $cfg_arc_click); if($typeid==0) { ShowMsg('請指定文檔的欄目!', '-1'); exit(); } if(empty($channelid)) { ShowMsg('文檔為非指定的類型,請檢查你發布內容的表單是否合法!', '-1'); exit(); } if(!CheckChannel($typeid,$channelid) ) { ShowMsg('你所選擇的欄目與當前模型不相符,請選擇白色的選項!', '-1'); exit(); } if(!TestPurview('a_New')) { CheckCatalog($typeid, "對不起,你沒有操作欄目 {$typeid} 的權限!"); } //對保存的內容進行處理 if(empty($writer)) $writer = $cuserLogin->getUserName(); if(empty($source)) $source = '未知'; $pubdate = GetMkTime($pubdate); $senddate = time(); $sortrank = AddDay($pubdate,$sortup); $ismake = $ishtml == 0 ? -1 : 0; $title = preg_replace("#\"#", '"', $title); $title = cn_substrR($title,$cfg_title_maxlen); $shorttitle = cn_substrR($shorttitle,36); $color = cn_substrR($color,7); $writer = cn_substrR($writer,20); $source = cn_substrR($source,30); $description = cn_substrR($description,$cfg_auot_description); $keywords = cn_substrR($keywords,60); $filename = trim(cn_substrR($filename,40)); $userip = GetIP(); $isremote = (empty($isremote)? 0 : $isremote); $voteid = (empty($voteid)? 0 : $voteid); $serviterm=empty($serviterm)? "" : $serviterm; if(!TestPurview('a_Check,a_AccCheck,a_MyCheck')) { $arcrank = -1; } $adminid = $cuserLogin->getUserID(); //處理上傳的縮略圖 if(empty($ddisremote)) { $ddisremote = 0; } $litpic = GetDDImage('none',$picname,$ddisremote); // 圖集相關--------------------- //使用第一張圖作為縮略圖 if($ddisfirst==1 && $litpic=='') { if(isset($imgurl1)) { $litpic = GetDDImage('ddfirst', $imgurl1, $isrm); } } // 圖集相關--------------------- //生成文檔ID $arcID = GetIndexKey($arcrank,$typeid,$sortrank,$channelid,$senddate,$adminid); if(empty($arcID)) { ShowMsg("無法獲得主鍵,因此無法進行后續操作!","-1"); exit(); } // 圖集相關--------------------- $imgurls = "{dede:pagestyle maxwidth='$maxwidth' pagepicnum='$pagepicnum' ddmaxwidth='$ddmaxwidth' row='$row' col='$col' value='$pagestyle'/}\r\n"; $hasone = FALSE; //處理並保存從網上復制的圖片 /*--------------------- function _getformhtml() ------------------*/ if($formhtml==1) { $imagebody = stripslashes($imagebody); $imgurls .= GetCurContentAlbum($imagebody,$copysource,$litpicname); if($ddisfirst==1 && $litpic=='' && !empty($litpicname)) { $litpic = $litpicname; $hasone = TRUE; } } /*--------------------- function _getformzip() 處理從ZIP中解壓的圖片 ---------------------*/ if($formzip==1) { include_once(DEDEINC."/zip.class.php"); include_once(DEDEADMIN."/file_class.php"); $zipfile = $cfg_basedir.str_replace($cfg_mainsite,'',$zipfile); $tmpzipdir = DEDEDATA.'/ziptmp/'.cn_substr(md5(ExecTime()),16); $ntime = time(); if(file_exists($zipfile)) { @mkdir($tmpzipdir,$GLOBALS['cfg_dir_purview']); @chmod($tmpzipdir,$GLOBALS['cfg_dir_purview']); $z = new zip(); $z->ExtractAll($zipfile,$tmpzipdir); $fm = new FileManagement(); $imgs = array(); $fm->GetMatchFiles($tmpzipdir,"jpg|png|gif",$imgs); $i = 0; foreach($imgs as $imgold) { $i++; $savepath = $cfg_image_dir."/".MyDate("Y-m",$ntime); CreateDir($savepath); $iurl = $savepath."/".MyDate("d",$ntime).dd2char(MyDate("His",$ntime).'-'.$adminid."-{$i}".mt_rand(1000,9999)); $iurl = $iurl.substr($imgold,-4,4); $imgfile = $cfg_basedir.$iurl; copy($imgold,$imgfile); unlink($imgold); if(is_file($imgfile)) { $litpicname = $pagestyle > 2 ? GetImageMapDD($iurl,$cfg_ddimg_width) : $iurl; //指定了提取第一張為縮略圖的情況強制使用第一張縮略圖 if($i=='1') { if(!$hasone && $ddisfirst==1 && $litpic=='' && empty($litpicname)) { $litpicname = GetImageMapDD($iurl,$cfg_ddimg_width); } } $info = ''; $imginfos = GetImageSize($imgfile,$info); $imgurls .= "{dede:img ddimg='$litpicname' text='' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img}\r\n"; //把圖片信息保存到媒體文檔管理檔案中 $inquery = " INSERT INTO #@__uploads(title,url,mediatype,width,height,playtime,filesize,uptime,mid) VALUES ('{$title}','{$iurl}','1','".$imginfos[0]."','".$imginfos[1]."','0','".filesize($imgfile)."','".$ntime."','$adminid'); "; $dsql->ExecuteNoneQuery($inquery); $fid = $dsql->GetLastID(); AddMyAddon($fid, $iurl); WaterImg($imgfile, 'up'); if(!$hasone && $ddisfirst==1 && $litpic=='') { if(empty($litpicname)) { $litpicname = $iurl; $litpicname = GetImageMapDD($iurl, $cfg_ddimg_width); } $litpic = $litpicname; $hasone = TRUE; } } } if($delzip==1) unlink($zipfile); $fm->RmDirFiles($tmpzipdir); } } /*--------------------- function _getformupload() 通過swfupload正常上傳的圖片 ---------------------*/ if(is_array($_SESSION['bigfile_info'])) { foreach($_SESSION['bigfile_info'] as $k=>$v) { $truefile = $cfg_basedir.$v; if(strlen($v)<2 || !file_exists($truefile)) continue; $info = ''; $imginfos = GetImageSize($truefile, $info); $litpicname = $pagestyle > 2 ? GetImageMapDD($v, $cfg_ddimg_width) : ''; if(!$hasone && $ddisfirst==1 && $litpic=='') { $litpic = empty($litpicname) ? GetImageMapDD($v, $cfg_ddimg_width) : $litpicname; $hasone = TRUE; } $imginfo = !empty(${'picinfook'.$k}) ? ${'picinfook'.$k} : ''; $imgurls .= "{dede:img ddimg='$v' text='$imginfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $v {/dede:img}\r\n"; } } $imgurls = addslashes($imgurls); //處理body字段自動摘要、自動提取縮略圖等 $body = AnalyseHtmlBody($body,$description,$litpic,$keywords,'htmltext'); // 圖集相關--------------------- //分析處理附加表數據 $inadd_f = $inadd_v = ''; if(!empty($dede_addonfields)) { $addonfields = explode(';', $dede_addonfields); if(is_array($addonfields)) { foreach($addonfields as $v) { if($v=='') continue; $vs = explode(',', $v); if($vs[1]=='htmltext' || $vs[1]=='textdata') { ${$vs[0]} = AnalyseHtmlBody(${$vs[0]}, $description, $litpic, $keywords, $vs[1]); } else { if(!isset(${$vs[0]})) ${$vs[0]} = ''; ${$vs[0]} = GetFieldValueA(${$vs[0]}, $vs[1], $arcID); } $inadd_f .= ','.$vs[0]; $inadd_v .= " ,'".${$vs[0]}."' "; } } } //處理圖片文檔的自定義屬性 if($litpic!='' && !preg_match("#p#", $flag)) { $flag = ($flag=='' ? 'p' : $flag.',p'); } if($redirecturl!='' && !preg_match("#j#", $flag)) { $flag = ($flag=='' ? 'j' : $flag.',j'); } //跳轉網址的文檔強制為動態 if(preg_match("#j#", $flag)) $ismake = -1; //保存到主表 $query = "INSERT INTO `#@__archives`(id,typeid,typeid2,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle, color,writer,source,litpic,pubdate,senddate,mid,voteid,notpost,description,keywords,filename,dutyadmin,weight) VALUES ('$arcID','$typeid','$typeid2','$sortrank','$flag','$ismake','$channelid','$arcrank','$click','$money','$title','$shorttitle', '$color','$writer','$source','$litpic','$pubdate','$senddate','$adminid','$voteid','$notpost','$description','$keywords','$filename','$adminid','$weight');"; if(!$dsql->ExecuteNoneQuery($query)) { $gerr = $dsql->GetError(); $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'"); ShowMsg("把數據保存到數據庫主表 `#@__archives` 時出錯,請把相關信息提交給DedeCms官方。".str_replace('"','',$gerr),"javascript:;"); exit(); } //保存到附加表 $cts = $dsql->GetOne("SELECT addtable FROM `#@__channeltype` WHERE id='$channelid' "); $addtable = trim($cts['addtable']); if(!empty($addtable)) { $useip = GetIP(); // $query = "INSERT INTO `{$addtable}`(aid,typeid,redirecturl,userip{$inadd_f}) Values('$arcID','$typeid','$redirecturl','$useip'{$inadd_v})"; // 圖集相關--------------------- $query = "INSERT INTO `{$addtable}`(aid,typeid,redirecturl,userip{$inadd_f},pagestyle,maxwidth,imgurls,row,col,isrm,ddmaxwidth,pagepicnum) Values('$arcID','$typeid','$redirecturl','$useip'{$inadd_v},'$pagestyle','$maxwidth','$imgurls','$row','$col','$isrm','$ddmaxwidth','$pagepicnum')"; // 圖集相關--------------------- if(!$dsql->ExecuteNoneQuery($query)) { $gerr = $dsql->GetError(); $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='$arcID'"); $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'"); ShowMsg("把數據保存到數據庫附加表 `{$addtable}` 時出錯,請把相關信息提交給DedeCms官方。".str_replace('"','',$gerr),"javascript:;"); exit(); } } //生成HTML InsertTags($tags, $arcID); if($cfg_remote_site=='Y' && $isremote=="1") { if($serviterm!="") { list($servurl,$servuser,$servpwd) = explode(',',$serviterm); $config=array( 'hostname' => $servurl, 'username' => $servuser, 'password' => $servpwd,'debug' => 'TRUE'); } else { $config=array(); } if(!$ftp->connect($config)) exit('Error:None FTP Connection!'); } $artUrl = MakeArt($arcID, true, true,$isremote); if($artUrl=='') { $artUrl = $cfg_phpurl."/view.php?aid=$arcID"; } ClearMyAddon($arcID, $title); //返回成功信息 $msg = " 請選擇你的后續操作: <a href='archives_add.php?cid=$typeid'><u>繼續發布文檔</u></a> <a href='$artUrl' target='_blank'><u>查看文檔</u></a> <a href='archives_do.php?aid=".$arcID."&dopost=editArchives'><u>更改文檔</u></a> <a href='catalog_do.php?cid=$typeid&dopost=listArchives'><u>已發布文檔管理</u></a> $backurl "; $msg = "<div style=\"line-height:36px;height:36px\">{$msg}</div>".GetUpdateTest(); $wintitle = '成功發布文檔!'; $wecome_info = '文檔管理::發布文檔'; $win = new OxWindow(); $win->AddTitle('成功發布文檔:'); $win->AddMsgItem($msg); $winform = $win->GetWindow('hand', ' ', false); $win->Display(); }
還是貼整個代碼吧修改的部分在注釋(// 圖集相關---------------------)之間
查詢數據庫,發現上傳成功。
后面的一些優化就根據自己的需求進行調整了!
