:::

7-2 上課範例:up_file.php

001<?php
002//version 1.0
003//2012-10-18
004//設定模組目錄名稱
005define("_MODDIR","contact");
006 
007 
008/*
009include_once("up_file.php");
010 
011表單: enctype='multipart/form-data'
012 
013<script src='" . TADTOOLS_URL . "/jquery/jquery.js'></script>
014<script src='" . TADTOOLS_URL . "/multiple-file-upload/jquery.MultiFile.js'></script>
015<input type='file' name='upfile[]' class='multi' maxlength='1' accept='gif|jpg|png|GIF|JPG|PNG'>".list_del_file("news_sn", $news_sn)."
016 
017儲存:upload_file($col_name, $col_sn, $width);
018 
019顯示:show_files($col_name, $col_sn,true,false,false,false);  //是否縮圖 , 顯示模式 filename、num , 顯示描述 , 顯示下載次數
020 
021單一相片:get_pic_file($col_name="", $col_sn="", $name="" , $sort="", $showkind="images")
022 
023 
024刪除:del_files($files_sn, $col_name, $col_sn);
025 
026檔案數量:get_file_amount($col_name="", $col_sn="");
027 
028 
029種類:img,file
030資料表:
031CREATE TABLE `files_center` (
032  `files_sn` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT '檔案流水號',
033  `col_name` varchar(255) NOT NULL COMMENT '欄位名稱',
034  `col_sn` smallint(5) unsigned NOT NULL COMMENT '欄位編號',
035  `sort` smallint(5) unsigned NOT NULL COMMENT '排序',
036  `kind` enum('img' , 'file') NOT NULL COMMENT '檔案種類',
037  `file_name` varchar(255) NOT NULL COMMENT '檔案名稱',
038  `file_type` varchar(255) NOT NULL COMMENT '檔案類型',
039  `file_size` int(10) unsigned NOT NULL COMMENT '檔案大小',
040  `description` text NOT NULL COMMENT '檔案說明',
041  `counter` mediumint(8) unsigned NOT NULL COMMENT '下載人次',
042  PRIMARY KEY (`files_sn`)
043) ENGINE=MyISAM COMMENT='檔案資料表';
044*/
045 
046 
047//檔案中心實體位置
048define("_FILES_CENTER_DIR",XOOPS_ROOT_PATH."/uploads/"._MODDIR . "/file");
049define("_FILES_CENTER_URL",XOOPS_URL."/uploads/"._MODDIR . "/file");
050//檔案中心圖片實體位置
051define("_FILES_CENTER_IMAGE_DIR",XOOPS_ROOT_PATH."/uploads/"._MODDIR . "/image");
052define("_FILES_CENTER_IMAGE_URL",XOOPS_URL."/uploads/"._MODDIR . "/image");
053//檔案中心縮圖實體位置
054define("_FILES_CENTER_THUMB_DIR",XOOPS_ROOT_PATH."/uploads/"._MODDIR . "/image/.thumbs");
055define("_FILES_CENTER_THUMB_URL",XOOPS_URL."/uploads/"._MODDIR . "/image/.thumbs");
056 
057//上傳圖檔,$col_name=對應欄位名稱, $col_sn=對應欄位編號, $種類:img,file, $sort=圖片排序, $files_sn="更新編號"
058function upload_file($col_name="", $col_sn="", $main_width="",$upfile='upfile', $sort="", $thumb_width="90", $files_sn=""){
059  global $xoopsDB, $xoopsUser, $xoopsModule;
060 
061  //引入上傳物件
062  include_once TADTOOLS_PATH."/upload/class.upload.php";
063 
064  //取消上傳時間限制
065  set_time_limit(0);
066  //設置上傳大小
067  ini_set('memory_limit', '80M');
068 
069 
070  //刪除勾選檔案
071  if(!empty($_POST['del_file'])){
072  foreach($_POST['del_file'] as $del_files_sn){
073      del_files($del_files_sn);
074    }
075  }
076 
077  $files = array();
078  foreach ($_FILES[$upfile] as $k => $l) {
079    foreach ($l as $i => $v) {
080      if (!array_key_exists($i, $files)){
081      $files[$i] = array();
082        }
083      $files[$i][$k] = $v;
084    }
085  }
086 
087  foreach ($files as $file) {
088    //先刪除舊檔
089    if(!empty($files_sn)){
090      del_files($files_sn);
091    }
092 
093    //自動排序
094    if(empty($sort)){
095      $sort=auto_sort($col_name, $col_sn);
096    }
097 
098    //取得檔案
099    $file_handle = new upload($file,"zh_TW");
100 
101    if ($file_handle->uploaded) {
102      //取得副檔名
103      $ext=strtolower($file_handle->file_src_name_ext);
104      //判斷檔案種類
105      if($ext=="jpg" or $ext=="jpeg" or $ext=="png" or $ext=="gif"){
106        $kind="img";
107      }else{
108        $kind="file";
109      }
110 
111      $file_handle->file_safe_name = false;
112      $file_handle->file_overwrite = true;
113      $file_handle->file_new_name_body   = "{$col_name}_{$col_sn}_{$sort}";
114      //若是圖片才縮圖
115      if($kind=="img"){
116        if($file_handle->image_src_x > $main_width){
117          $file_handle->image_resize     = true;
118          $file_handle->image_x        = $main_width;
119          $file_handle->image_ratio_y     = true;
120        }
121      }
122      $path=($kind=="img")?_FILES_CENTER_IMAGE_DIR:_FILES_CENTER_DIR;
123      $file_handle->process($path);
124      $file_handle->auto_create_dir = true;
125 
126 
127      //若是圖片才製作小縮圖
128      if($kind=="img"){
129        $file_handle->file_safe_name = false;
130        $file_handle->file_overwrite = true;
131        $file_handle->file_new_name_body   = "{$col_name}_{$col_sn}_{$sort}";
132        if($file_handle->image_src_x > $thumb_width){
133          $file_handle->image_resize     = true;
134          $file_handle->image_x        = $thumb_width;
135          $file_handle->image_ratio_y     = true;
136        }
137        $file_handle->process(_FILES_CENTER_THUMB_DIR);
138        $file_handle->auto_create_dir = true;
139      }
140 
141 
142      //上傳檔案
143      if ($file_handle->processed) {
144        $file_handle->clean();
145        $file_name="{$col_name}_{$col_sn}_{$sort}.{$ext}";
146 
147        if(empty($files_sn)){
148          $sql = "insert into ".$xoopsDB->prefix(_MODDIR . "_files_center")." (`col_name` , `col_sn` , `sort` , `kind` , `file_name` , `file_type` , `file_size` , `description`) values('$col_name' , '$col_sn' , '$sort' , '{$kind}' , '{$file_name}' , '{$file['type']}' , '{$file['size']}' , '{$file['name']}')";
149          $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
150        }else{
151          $sql = "replace into ".$xoopsDB->prefix(_MODDIR . "_files_center")." (`files_sn` , `col_name` , `col_sn` , `sort` , `kind` , `file_name` , `file_type` , `file_size` , `description`) values('{$files_sn}' , '$col_name' , '$col_sn' , '$sort' , '{$kind}' , '{$file_name}' , '{$file['type']}' , '{$file['size']}' , '{$file['name']}')";
152          $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
153        }
154      } else {
155        redirect_header($_SERVER['PHP_SELF'],3, "Error:".$file_handle->error);
156      }
157    }
158    $sort="";
159  }
160}
161 
162 
163//刪除實體檔案
164function del_files($files_sn="", $col_name="", $col_sn="", $sort=""){
165  global $xoopsDB, $xoopsUser;
166 
167  if(!empty($files_sn)){
168    $del_what="`files_sn`='{$files_sn}'";
169  }elseif(!empty($col_name) and !empty($col_sn)){
170    $and_sort=(empty($sort))?"":"and `sort`='{$sort}'";
171    $del_what="`col_name`='{$col_name}' and `col_sn`='{$col_sn}' $and_sort";
172  }
173 
174  $sql = "select * from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where $del_what";
175  $result=$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error()."<br>".$sql);
176 
177  while(list($files_sn, $col_name, $col_sn, $sort, $kind, $file_name, $file_type, $file_size, $description)=$xoopsDB->fetchRow($result)){
178    $del_sql = "delete  from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where files_sn='{$files_sn}'";
179    $xoopsDB->queryF($del_sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
180 
181    if($kind=="img"){
182      unlink(_FILES_CENTER_IMAGE_DIR . "/$file_name");
183      unlink(_FILES_CENTER_THUMB_DIR . "/$file_name");
184    }else{
185      unlink(_FILES_CENTER_DIR . "/$file_name");
186    }
187  }
188}
189 
190 
191//取得檔案 $kind=images(大圖),thumb(小圖),$mode=link(完整連結)or array(路徑陣列)
192function get_file($col_name="", $col_sn="", $sort=""){
193  global $xoopsDB, $xoopsUser, $xoopsModule;
194 
195  $and_sort=(!empty($sort))?" and `sort`='{$sort}'":"";
196 
197  $sql = "select * from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where `col_name`='{$col_name}' and `col_sn`='{$col_sn}' $and_sort order by sort";
198 
199  $result=$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
200  while($all=$xoopsDB->fetchArray($result)){
201    //以下會產生這些變數: $files_sn, $col_name, $col_sn, $sort, $kind, $file_name, $file_type, $file_size, $description
202    foreach($all as $k=>$v){
203      $$k=$v;
204    }
205 
206    $files[$files_sn]['kind']=$kind;
207    $files[$files_sn]['sort']=$sort;
208    $files[$files_sn]['file_name']=$file_name;
209    $files[$files_sn]['file_type']=$file_type;
210    $files[$files_sn]['file_size']=$file_size;
211    $files[$files_sn]['counter']=$counter;
212    $files[$files_sn]['description']=$description;
213 
214    if($kind=="img"){
215      $pic_name=(file_exists(_FILES_CENTER_IMAGE_DIR . "/{$file_name}"))?_FILES_CENTER_IMAGE_URL."/{$file_name}":TADTOOLS_URL."/multiple-file-upload/no_thumb.gif";
216      $thumb_pic=(file_exists(_FILES_CENTER_THUMB_DIR . "/{$file_name}"))?_FILES_CENTER_THUMB_URL."/{$file_name}":TADTOOLS_URL."/multiple-file-upload/no_thumb.gif";
217      $files[$files_sn]['link']="<a href='{$_SERVER['PHP_SELF']}?fop=dl&files_sn=$files_sn' title='{$description}' rel='lytebox'><img src='{$pic_name}' alt='{$description}' title='{$description}' rel='lytebox'></a>";
218      $files[$files_sn]['path']=$pic_name;
219      $files[$files_sn]['url']="<a href='{$_SERVER['PHP_SELF']}?fop=dl&files_sn=$files_sn' title='{$description}' target='_blank'>{$description}</a>";
220      $files[$files_sn]['tb_link']="<a href='{$_SERVER['PHP_SELF']}?fop=dl&files_sn=$files_sn' title='{$description}' rel='lytebox'><img src='$thumb_pic' alt='{$description}' title='{$description}'></a>";
221      $files[$files_sn]['tb_path']=$thumb_pic;
222      $files[$files_sn]['tb_url']="<a href='{$_SERVER['PHP_SELF']}?fop=dl&files_sn=$files_sn' title='{$description}' rel='lytebox'>{$description}</a>";
223    }else{
224      $files[$files_sn]['link']="<a href='{$_SERVER['PHP_SELF']}?fop=dl&files_sn=$files_sn'>{$description}</a>";
225      $files[$files_sn]['path']="{$_SERVER['PHP_SELF']}?fop=dl&files_sn=$files_sn";
226    }
227  }
228  return $files;
229}
230 
231 
232//取得單一圖片 $kind=images(大圖),thumb(小圖)
233function get_pic_file($col_name="", $col_sn="", $sort="", $showkind="images"){
234  global $xoopsDB, $xoopsUser, $xoopsModule;
235 
236  $and_sort=(!empty($sort))?" and `sort`='{$sort}'":"";
237 
238  $sql = "select * from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where `col_name`='{$col_name}' and `col_sn`='{$col_sn}' $and_sort order by sort limit 0,1";
239 
240  $result=$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
241  while($all=$xoopsDB->fetchArray($result)){
242    //以下會產生這些變數: $files_sn, $col_name, $col_sn, $sort, $kind, $file_name, $file_type, $file_size, $description
243    foreach($all as $k=>$v){
244      $$k=$v;
245    }
246 
247    if($showkind=="thumb"){
248      $files=(file_exists(_FILES_CENTER_THUMB_DIR . "/{$file_name}"))?_FILES_CENTER_THUMB_URL."/{$file_name}":"";
249    }else{
250      $files=(file_exists(_FILES_CENTER_IMAGE_DIR . "/{$file_name}"))?_FILES_CENTER_IMAGE_URL."/{$file_name}":"";
251    }
252  }
253  return $files;
254}
255 
256 
257//取得檔案數
258function get_file_amount($col_name="", $col_sn=""){
259  global $xoopsDB, $xoopsUser, $xoopsModule;
260 
261  $sql = "select count(*) from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where `col_name`='{$col_name}' and `col_sn`='{$col_sn}'";
262 
263  $result=$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
264  list($amount)=$xoopsDB->fetchRow($result);
265  return $amount;
266}
267 
268 
269//列出可刪除檔案
270function list_del_file($col_name="", $col_sn=""){
271  global $xoopsDB, $xoopsUser, $xoopsModule;
272 
273  $all_file="";
274  $sql = "select * from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where `col_name`='{$col_name}' and `col_sn`='{$col_sn}' order by sort";
275 
276  $result=$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
277  while($all=$xoopsDB->fetchArray($result)){
278    //以下會產生這些變數: $files_sn, $col_name, $col_sn, $sort, $kind, $file_name, $file_type, $file_size, $description
279    foreach($all as $k=>$v){
280      $$k=$v;
281    }
282    $all_file.="<input type='checkbox' name='del_file[]' value='{$files_sn}'> $description<br>";
283  }
284 
285  if(empty($all_file))return;
286 
287  $files.="<div>選取欲刪除檔案<br>$all_file</div>";
288  return $files;
289}
290 
291 
292//取得附檔或附圖 $show_mode=filename、num
293function show_files($col_name="" , $col_sn="" , $thumb=true , $show_mode="" , $show_description=false , $show_dl=false){
294  if($show_mode==""){
295    $all_files="<script type='text/javascript' language='javascript' src='" . TADTOOLS_URL . "/lytebox/lytebox.js'></script>
296    <link rel='stylesheet' href='" . TADTOOLS_URL . "/lytebox/lytebox.css' type='text/css' media='screen' />";
297  }else{
298    $all_files="";
299  }
300  $file_arr="";
301  $file_arr=get_file($col_name, $col_sn);
302  if(empty($file_arr))return;
303 
304  if($file_arr){
305    $i=1;
306    foreach($file_arr as $files_sn => $file_info){
307      if($show_mode=="filename"){
308        if($file_info['kind']=="file"){
309          $all_files.="<div>({$i}) {$file_info['link']}</div>";
310        }else{
311          $all_files.="<div>({$i}) {$file_info['url']}</div>";
312        }
313      }else{
314        if($file_info['kind']=="file"){
315          $linkto=$file_info['path'];
316          $description=$file_info['description'];
317          $thumb_pic=TADTOOLS_URL."/multiple-file-upload/downloads.png";
318          $rel="";
319        }else{
320          $linkto=$file_info['path'];
321          $description=$file_info['description'];
322          $thumb_pic=($thumb)?$file_info['tb_path']:$file_info['path'];
323          $rel="rel='lyteshow[{$col_name}_{$course_sn}]' title='{$description}'";
324        }
325 
326        //描述顯示
327        $show_description_txt=($show_description)?"<div style='height:40px;font-size:9pt;font-weight:normal;overflow:hidden;text-align:center;'><a href='{$linkto}' $rel style='font-size:9pt;font-weight:normal;'>{$description}</a></div>":"";
328 
329        //下載次數顯示
330        $show_dl_txt=($show_dl)?"<img src='" . TADTOOLS_URL . "/multiple-file-upload/dl_times.gif' alt='download counter' title='download counter' align='absmiddle' hspace=4>: {$file_info['counter']}":"";
331 
332        $width=($thumb)?110:400;
333        $pic_height=($thumb)?90:300;
334        $height=($thumb)?100:320;
335        $height+=($show_description)?30:0;
336 
337        $all_files.="
338        <div style='border:0px solid gray;width:{$width}px;height:{$height}px;float:left;display:inline;margin:2px;'>
339          <a href='{$linkto}' $rel>
340          <div align='center' style=\"border:1px solid #CFCFCF;width:{$width}px;height:{$pic_height}px;overflow:hidden;margin:2px auto;background-image:url('{$thumb_pic}');background-repeat: no-repeat;background-position: center center;cursor:pointer;\">
341          $show_dl_txt
342          </div>
343        </a>
344        $show_description_txt
345        </div>";
346      }
347 
348    $i++;
349    }
350  }else{
351    $all_files="";
352  }
353  $all_files.="<div style='clear:both;'></div>";
354  return $all_files;
355}
356 
357 
358//取得單一檔案資料
359function get_one_file($files_sn=""){
360  global $xoopsDB, $xoopsUser, $xoopsModule;
361 
362  $sql = "select * from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where `files_sn`='{$files_sn}'";
363  $result=$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
364  $all=$xoopsDB->fetchArray($result);
365  return $all;
366}
367 
368 
369//自動編號
370function auto_sort($col_name="", $col_sn=""){
371  global $xoopsDB, $xoopsUser;
372 
373  $sql = "select max(sort) from ".$xoopsDB->prefix(_MODDIR . "_files_center")." where `col_name`='{$col_name}' and `col_sn`='{$col_sn}'";
374  $result=$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
375  list($max)=$xoopsDB->fetchRow($result);
376  return ++$max;
377}
378 
379 
380//下載並新增計數器
381function add_file_counter($files_sn=""){
382  global $xoopsDB;
383 
384  $file=get_one_file($files_sn);
385  $sql = "update ".$xoopsDB->prefix(_MODDIR . "_files_center")." set `counter`=`counter`+1 where `files_sn`='{$files_sn}'";
386  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
387 
388  if($file['kind']=="img"){
389    header("location:"._FILES_CENTER_IMAGE_URL."/{$file['file_name']}");
390  }else{
391    header("location:"._FILES_CENTER_URL."/{$file['file_name']}");
392  }
393}
394 
395 
396if($_GET['fop']=="dl"){
397  add_file_counter($_GET['files_sn']);
398}
399?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Fmail.tad0616.cp22.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D768%26tbsn%3D26

書籍目錄

展開 | 闔起

線上使用者

93人線上 (2人在瀏覽線上書籍)

會員: 0

訪客: 93

更多…