PHP非常實用的上傳類,上傳效果線上示範

來源:互聯網
上載者:User

 

  #*********************************************************

  #檔案名稱:

  inc_class.upload.php

  #Copyright (c)

  2007-2009 青春一度 all rights reserved.

  #最後更新: 2009-08-05

  #版本 : v 2.0.a

  #註:轉寄時請保留此聲明資訊,這段聲明不並會影響你的速度!

  #如有修改請將修改後的檔案以郵件形式發送給作者一份,謝謝!

  #

  #*********************************************************

  if(!defined('IN_PHPADLEYIU'))

  {

  exit('Access Denied');

  }

  /*

  //使用說明:

  //聲明一個上傳類

  include_once(ADLEYLIU_ROOT.'./inc_class.upload.php');

  $_YL_UPLOAD

  = array();

  $yl_uploadfile = new

  yl_upload_class();

  $_YL_UPLOAD['yl_filedata'] =

  'uploadFile';//表單名

  $_YL_UPLOAD['yl_directroy'] =

  'upload_files';//上傳主目錄

  $_YL_UPLOAD['file_urldirectroy'] = '/';//

  程式路徑

  $_YL_UPLOAD['yl_settingsnew'] =

  ''.date('ym').'/'.date('d').'/'.substr(time(), 0,

  5).'';//上傳子主目錄

  $_YL_UPLOAD['yl_maxsize'] = 1048576;

  //這裡以位元組為單位(1024*2)*1024=2097152 就是 2M

  $_YL_UPLOAD['yl_sizeformat'] =

  'k'; //顯示檔案大小單位b位元組,k千,m兆

  $_YL_UPLOAD['yl_arrext'] =

  array('gif','jpg','jpeg','png','bmp','rar','txt');//允許上傳檔案類型

  $_YL_UPLOAD['yl_ext'] = 0; //0原檔案類型上傳,1統一為存為jpg

  $_YL_UPLOAD['yl_prefix'] = ''.$uid.''.$yl_uploadfile -> yl_createrand(1,0).''.$cid.'';

  //在檔案名稱首碼加上特殊字元 //$uid 會員ID $cid 分類ID

  $_YL_UPLOAD['yl_suffix'] = ''; //''.$yl_uploadfile -> yl_createrand(3,0).'';

  //在檔案名稱尾碼加上特殊字元

  $_YL_UPLOAD['thumbwidth'] = 100;

  //縮圖寬

  $_YL_UPLOAD['thumbheight'] = 100;

  //縮圖高

  $_YL_UPLOAD['maxthumbwidth'] = 500;

  //大圖高

  $_YL_UPLOAD['maxthumbheight'] = 500;

  //大圖寬

  //上傳

  $yl_uploadfile -> yl_uploadfile();

  擷取值:

  'yl_filename' => addslashes($_YL_UPLOAD['yl_filename']),原檔案名稱

  'yl_attachment' => $_YL_UPLOAD['yl_attachment'],新檔案名稱及路徑

  'yl_filesize' => $_YL_UPLOAD['yl_filesize'] ,檔案大小

  'yl_filetype' => $_YL_UPLOAD['yl_filetype'],檔案類型

  'yl_isimage' => $_YL_UPLOAD['yl_isimage'],是否是圖片

  'yl_isthumb' => $_YL_UPLOAD['yl_isthumb'],是否有小圖片

  */

  class yl_upload_class

  {

  function __GET($property_name)

  {

  if(isset($this -> $property_name))

  {

  return $this -> $property_name;

  } else

  {

  return

  NULL;

  }

  }

  function __SET($property_name,

  $value) {

  $this -> $property_name =

  $value;

  }

  #*********************************************************

  #產生縮圖

  #*********************************************************

  function makethumb($srcfile) {

  global $_YL_UPLOAD;

  //判斷檔案是否存在

  if (!file_exists($srcfile))

  {

  return '';

  }

  $dstfile =

  $srcfile.'.small.jpg';

  $bigfile =

  $srcfile.'.big.jpg';

  //縮圖大小

  $tow =

  intval($_YL_UPLOAD['thumbwidth']);

  $toh =

  intval($_YL_UPLOAD['thumbheight']);

  if($tow < 60) $tow =

  60;

  if($toh < 60) $toh = 60;

  $make_max = 0;

  $maxtow =

  intval($_YL_UPLOAD['maxthumbwidth']);

  $maxtoh =

  intval($_YL_UPLOAD['maxthumbheight']);

  if($maxtow >= 300

  && $maxtoh >= 300) {

  $make_max =

  1;

  }

  //擷取圖片資訊

  $im = '';

  if($data =

  getimagesize($srcfile)) {

  if($data[2] == 1)

  {

  $make_max =

  0;//gif不處理

  if(function_exists("imagecreatefromgif"))

  {

  $im =

  imagecreatefromgif($srcfile);

  }

  }

  elseif($data[2] == 2)

  {

  if(function_exists("imagecreatefromjpeg"))

  {

  $im =

  imagecreatefromjpeg($srcfile);

  }

  }

  elseif($data[2] == 3)

  {

  if(function_exists("imagecreatefrompng"))

  {

  $im =

  imagecreatefrompng($srcfile);

  }

  }

  }

  if(!$im)

  return '';

  $srcw = imagesx($im);

  $srch = imagesy($im);

  $towh = $tow/$toh;

  $srcwh =

  $srcw/$srch;

  if($towh <= $srcwh){

  $ftow =

  $tow;

  $ftoh = $ftow*($srch/$srcw);

  $fmaxtow = $maxtow;

  $fmaxtoh =

  $fmaxtow*($srch/$srcw);

  } else {

  $ftoh =

  $toh;

  $ftow = $ftoh*($srcw/$srch);

  $fmaxtoh = $maxtoh;

  $fmaxtow =

  $fmaxtoh*($srcw/$srch);

  }

  if($srcw <= $maxtow

  && $srch <= $maxtoh) {

  $make_max =

  0;//不處理

  }

  if($srcw > $tow $srch > $toh)

  {

  if(function_exists("imagecreatetruecolor") &&

  function_exists("imagecopyresampled") && @$ni =

  imagecreatetruecolor($ftow, $ftoh))

  {

  imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow,

  $ftoh, $srcw,

  $srch);

  //大圖片

  if($make_max

  && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh))

  {

  imagecopyresampled($maxni, $im, 0, 0, 0, 0,

  $fmaxtow, $fmaxtoh, $srcw, $srch);

  }else if (@$maxni

  = imagecreatetruecolor(round($srcw/2),

  round($srch/2))){

  imagecopyresampled($maxni,

  $im, 0, 0, 0, 0, round($srcw/2), round($srch/2), $srcw,

  $srch);

  }

  }

  elseif(function_exists("imagecreate") &&

  function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh))

  {

  imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow,

  $ftoh, $srcw,

  $srch);

  //大圖片

  if($make_max

  && @$maxni = imagecreate($fmaxtow, $fmaxtoh))

  {

  imagecopyresized($maxni, $im, 0, 0, 0, 0,

  $fmaxtow, $fmaxtoh, $srcw, $srch);

  }else if (@$maxni

  = imagecreate(round($srcw/2),

  round($srch/2))){

  imagecopyresized($maxni, $im,

  0, 0, 0, 0, round($srcw/2), round($srch/2), $srcw,

  $srch);

  }

  } else

  {

  return

  '';

  }

  if(function_exists('imagejpeg'))

  {

  imagejpeg($ni,

  $dstfile);

  //大圖片

  if($make_max)

  {

  imagejpeg($maxni,

  $bigfile);

  }else{

  imagejpeg($maxni,

  $bigfile);

  }

  }

  elseif(function_exists('imagepng')) {

  imagepng($ni,

  $dstfile);

  //大圖片

  if($make_max)

  {

  imagepng($maxni,

  $bigfile);

  }else{

  imagejpeg($maxni,

  $bigfile);

  }

  }

  imagedestroy($ni);

  if($make_max)

  {

  }else{

  imagedestroy($maxni);

  }

  }else{

  if(function_exists("imagecreatetruecolor") &&

  function_exists("imagecopyresampled") && @$ni =

  imagecreatetruecolor($srcw, $srch))

  {

  imagecopyresampled($ni, $im, 0, 0, 0, 0, $srcw,

  $ftoh, $srch,

  $srch);

  //大圖片

  $maxni =

  imagecreatetruecolor($srch, $srch);

  imagecopyresampled($maxni, $im, 0, 0, 0, 0, $srcw, $srch, $srcw,

  $srch);

  } elseif(function_exists("imagecreate")

  && function_exists("imagecopyresized") && @$ni =

  imagecreate($ftow, $ftoh)) {

  imagecopyresized($ni,

  $im, 0, 0, 0, 0, $srcw, $srch, $srcw,

  $srch);

  //大圖片

  $maxni =

  imagecreate($fmaxtow, $fmaxtoh);

  imagecopyresized($maxni, $im, 0, 0, 0, 0, $srcw, $srch, $srcw,

  $srch);

  } else

  {

  return

  '';

  }

  imagejpeg($ni, $dstfile);

  imagejpeg($maxni,

  $bigfile);

  }

  imagedestroy($im);

  if(!file_exists($dstfile)) {

  return

  '';

  } else {

  return

  $dstfile;

  }

  }

  #*********************************************************

  #擷取隨機數函數

  #*********************************************************

  function

  yl_createrand($length, $numeric = 0) {

  PHP_VERSION < '4.2.0'

  && mt_srand((double)microtime() * 1000000);

  if($numeric)

  {

  $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10,

  $length) - 1));

  } else {

  $hash =

  '';

  $chars =

  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';///0123456789

  $max

  = strlen($chars) - 1;

  for($i = 0; $i < $length; $i++)

  {

  $hash .= $chars[mt_rand(0,

  $max)];

  }

  }

  return

  $hash;

  }

  #***************

  #*********************************************************

  #建立目錄函式

  #*********************************************************

  function

  createfolder($yl_path)

  {

  if

  (!file_exists($yl_path))

  {

  $this ->

  createfolder(dirname($yl_path));

  @mkdir($yl_path,

  0777);

  }

  return $this ->

  createfolder;

  }

  #*********************************************************

  #擷取檔案

  名稱,大小,類型,臨時檔案名稱

  #*********************************************************

  function

  yl_getfilename($yl_type)

  {

  global

  $_YL_UPLOAD;

  return

  $_FILES[$_YL_UPLOAD['yl_filedata']][$yl_type];

  }

  #*********************************************************

  #擷取檔案大小

  #*********************************************************

  function

  yl_getfilesize()

  {

  global

  $_YL_UPLOAD;

  $yl_filesize = $this ->

  yl_getfilename('size');

  if($yl_filesize ==

  0){

  $this ->

  alert("請選擇上傳檔案!");

  exit;

  }

  if($yl_filesize

  > $_YL_UPLOAD['yl_maxsize']){

  switch

  (strtolower($_YL_UPLOAD['yl_sizeformat'])){

  case

  'b':

  $yl_maxsizek = $_YL_UPLOAD['yl_maxsize'] .

  ' B';

  break;

  case

  'k':

  $yl_maxsizek =

  $_YL_UPLOAD['yl_maxsize']/1024 . '

  K';

  break;

  case

  'm':

  $yl_maxsizek =

  $_YL_UPLOAD['yl_maxsize']/(1024*1024) . '

  M';

  }

  $this ->

  alert("上傳檔案超出限制範圍[".$yl_maxsizek."].K!");

  exit;

  }

  return

  $yl_filesize;

  }

  #*********************************************************

  #獲得副檔名

  #*********************************************************

  function

  yl_getfiletype()

  {

  global

  $_YL_UPLOAD;

  $pathinfo = pathinfo($this -> yl_getfilename('name'));

  $yl_file_ext =

  strtolower($pathinfo['extension']);

  //檢查副檔名

  if(!array_keys($_YL_UPLOAD['yl_arrext'],$yl_file_ext))

  {

  $this ->

  alert("上傳檔案類型被限制!");

  exit;

  }

  return

  $yl_file_ext;

  }

  #*********************************************************

  #上傳驗證

  #*********************************************************

  function

  yl_upfile($source, $target) {

  //

  如果一種函數上傳失敗,還可以用其他函數上傳

  if (function_exists('move_uploaded_file')

  && @move_uploaded_file($source, $target))

  {

  @chmod($target, 0666);

  return

  $target;

  } elseif (@copy($source, $target))

  {

  @chmod($target, 0666);

  return

  $target;

  } elseif (@is_readable($source))

  {

  if ($fp = @fopen($source,'rb'))

  {

  @flock($fp,2);

  $filedata

  =

  @fread($fp,@filesize($source));

  @fclose($fp);

  }

  if

  ($fp = @fopen($target, 'wb')) {

  @flock($fp,

  2);

  @fwrite($fp,

  $filedata);

  @fclose($fp);

  @chmod

  ($target, 0666);

  return

  $target;

  } else {

  return

  false;

  }

  }

  }

  #*********************************************************

  #上傳

  #*********************************************************

  function

  yl_uploadfile()

  {

  global $_YL_UPLOAD;

  $yl_file_path = $_YL_UPLOAD['yl_directroy'].'/'.$_YL_UPLOAD['yl_settingsnew'] ;//建立一個目錄

  $yl_filename = $this -> yl_getfilename('name');//原檔案名稱

  $yl_filenamenews = $_YL_UPLOAD['yl_prefix'].''.substr(time(), 5, 9).''.$_YL_UPLOAD['yl_suffix'].'';//重新命名

  $yl_file_size = $this -> yl_getfilesize();//擷取檔案大小

  $yl_file_type = $this -> yl_getfiletype();//擷取檔案類型

  if($_YL_UPLOAD['yl_ext'] ==

  0){

  $yl_filenamenewsext = $yl_filenamenews.'.'.$yl_file_type;//改名

  }elseif ($_YL_UPLOAD['yl_ext'] == 1){

  $yl_filenamenewsext = $yl_filenamenews.'.jpg';//統一改名為jpg

  }

  //$yl_tmp_name = str_replace(' ','',$this ->

  yl_getfilename('tmp_name'));//伺服器上臨時檔案名稱

  $yl_tmp_name = $this -> yl_getfilename('tmp_name');//伺服器上臨時檔案名稱

  //檢查是否已上傳

  if(

  href="!@is_uploaded_file($yl_tmp_name" _cke_saved_href="[email=!@is_uploaded_file($yl_tmp_name]!@is_uploaded_file($yl_tmp_name">mailto:!@is_uploaded_file($yl_tmp_name">!@is_uploaded_file($yl_tmp_name))

  {

  $this ->

  alert("檔案已上傳!");

  exit;

  }

  //檢查目錄是否存在,不存在則建立

  if(

  href="!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''" _cke_saved_href="[email=!@is_dir(]!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">mailto:!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''))

  {

  $this ->

  createfolder(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.'');//建立目錄

  }

  //檢查目錄寫入權限

  if

  (

  href="!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''" _cke_saved_href="[email=!@is_writable(]!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">mailto:!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''))

  {

  $this ->

  alert("上傳目錄沒有寫入權限!");

  exit;

  }

  $yl_path_name

  =

  ''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.'/'.$yl_filenamenewsext.'';

  $yl_doupload = $this -> yl_upfile($yl_tmp_name, $yl_path_name);

  if($yl_doUpload === false){

  $this -> alert("上傳失敗!");

  exit;

  }else{

  //echo

  '上傳成功';

  //echo

  '
';

  /*

  echo

  '原檔案名稱:'.$yl_filename.'';

  echo

  '
';

  echo

  '新檔案名稱及目錄:'.$yl_file_path.'/'.$yl_filenamenewsext;

  echo

  '
';

  echo

  '檔案大小:'.$yl_file_size.'';

  echo '
';

  echo '檔案類型:'.$yl_file_type.'';

  */

  $_YL_UPLOAD['yl_filename'] = $yl_filename;

  $_YL_UPLOAD['yl_attachment'] = ''.$yl_file_path.'/'.$yl_filenamenewsext.'';

  $_YL_UPLOAD['yl_filesize'] = $yl_file_size;

  $_YL_UPLOAD['yl_filetype'] = $yl_file_type;

  //檢查是否圖片

  if(@getimagesize($yl_path_name))

  {

  $_YL_UPLOAD['yl_isimage'] =

  1;

  ///產生縮圖

  if ($this -> makethumb($yl_path_name)){

  $_YL_UPLOAD['yl_isthumb'] = 1;

  }else{

  $_YL_UPLOAD['yl_isthumb'] = 0;

  }

  }else{

  $_YL_UPLOAD['yl_isimage'] = 0;

  }

  }

  return

  true;

  }

  #*********************************************************

  #提示

  #*********************************************************

  function

  alert($yl_msg)

  {

  echo '';

  echo '';

  echo '';

  echo '';

  echo '';

  echo '';

  echo ' <script></script>';

  echo '';

  echo '';

  exit;

  }

  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.