ホームページ  >  記事  >  php教程  >  PHP は非常に実用的なアップロード クラスであり、アップロードの効果はオンラインで実証されます。

PHP は非常に実用的なアップロード クラスであり、アップロードの効果はオンラインで実証されます。

WBOY
WBOYオリジナル
2016-06-21 08:51:30900ブラウズ

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

#ファイル名:

inc_class.upload.php

#著作権 (c)

2007-2009 ワンスユース all Rights Reserved.

#最終更新日: 2009-08-05

#バージョン: v 2.0.a

#注意: このステートメントの情報は転送時に保存してください。このステートメントは速度には影響しません。

#変更がある場合は、変更したファイルのコピーを電子メールで作成者に送信してください。

#

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

if(!define('IN_PHPADLEYIU'))

{

exit('アクセスが拒否されました');

}

/*

//使用説明:

//アップロードクラスを宣言します

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

$_YL_UPLOAD

= 配列();

$yl_uploadfile = 新しい

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_createand(1,0).''.$cid.'';

//ファイル名のプレフィックスに特殊文字を追加します //$uid メンバー ID $cid カテゴリ ID

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

//特殊文字

をファイル名の接尾辞に追加します

$_YL_UPLOAD['サム幅'] = 100;

//サムネイルの幅

$_YL_UPLOAD['親指の高さ'] = 100;

//サムネイルの高さ

$_YL_UPLOAD['maxthumbwidth'] = 500;

//大きな画像の高さ

$_YL_UPLOAD['maxthumbheight'] = 500;

//画像幅が大きい

//アップロード

$yl_uploadfile -> yl_uploadfile();

値の取得:

'yl_filename' => ラッシュを追加します($_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']、小さな画像があるかどうか

*/

クラス yl_upload_class

{

関数 __GET($property_name)

{

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

{

$this -> $property_name;

を返します

} 他

{

戻る

NULL;

}

}

関数 __SET($プロパティ名,

$value) {

$this -> $property_name =

$value;

}

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

#サムネイルを生成

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

関数 makethumb($srcfile) {

グローバル $_YL_UPLOAD;

//ファイルが存在するかどうかを判断します

if (!file_exists($srcfile))

{

'';

を返します

}

$dstfile =

$srcfile.'.small.jpg';

$bigfile =

$srcfile.'.big.jpg';

//サムネイルのサイズ

$tow =

intval($_YL_UPLOAD['thumbwidth']);

$toh =

intval($_YL_UPLOAD['親指の高さ']);

if($tow

60;

if($toh

  $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)

  '';

を返します

  $srcw = 画像x($im);

  $srch = imagey($im);

  $towh = $tow/$toh;

  $srcwh =

  $srcw/$srch;

  if($towh

  $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

  && $srch

  $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("imagecopyresize") && @$ni = imagecreate($ftow, $ftoh))

  {

  imagecopyresize($ni, $im, 0, 0, 0, 0, $ftow,

  $ftoh、$srcw、

  $srch);

  //大写真

  if($make_max

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

  {

  imagecopyresize($maxni, $im, 0, 0, 0, 0,

  $fmaxtow、$fmaxtoh、$srcw、$srch);

  }else if (@$maxni

  = imagecreate(round($srcw/2),

  Round($srch/2))){

  imagecopyresize($maxni, $im,

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

  $srch);

  }

  } 他

  {

  戻る

  '';

  }

  if(function_exists('imagejpeg'))

  {

  imagejpeg($ni,

  $dstfile);

  //大写真

  if($make_max)

  {

  imagejpeg($maxni,

  $bigfile);

  }その他{

  imagejpeg($maxni,

  $bigfile);

  }

  }

  elseif(function_exists('imagepng')) {

  imagepng($ni,

  $dstfile);

  //大写真

  if($make_max)

  {

  imagepng($maxni,

  $bigfile);

  }その他{

  imagejpeg($maxni,

  $bigfile);

  }

  }

  imagedestroy($ni);

  if($make_max)

  {

  }その他{

  imagedestroy($maxni);

  }

  }その他{

  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("imagecopyresize") && @$ni =

  imagecreate($ftow, $ftoh)) {

  imagecopyresize($ni,

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

  $srch);

  //大写真

  $maxni =

  imagecreate($fmaxtow, $fmaxtoh);

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

  $srch);

  } 他

  {

  戻る

  '';

  }

  imagejpeg($ni, $dstfile);

  imagejpeg($maxni,

  $bigfile);

  }

  imagedestroy($im);

  if(!file_exists($dstfile)) {

  戻る

  '';

  } else {

  戻る

  $dstfile;

  }

  }

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

  #获取随机数関数

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

  関数

  yl_createand($length, $numeric = 0) {

  PHP_VERSION

  && 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

  {

  $hash .= $chars[mt_rand(0,

  $max)];

  }

  }

  戻る

  $hash;

  }

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

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

  #创建目录関数数

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

  関数

  createfolder($yl_path)

  {

  もし

  (!file_exists($yl_path))

  {

  $this ->

  createfolder(dirname($yl_path));

  @mkdir($yl_path,

  0777);

  }

  $this を返す ->

  フォルダーの作成;

  }

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

  #获取文件

  名前,大きさ,種類,時刻文ファイル名

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

  関数

  yl_getfilename($yl_type)

  {

  グローバル

  $_YL_UPLOAD;

  戻る

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

  }

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

  #获取文件大小

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

  関数

  yl_getfilesize()

  {

  グローバル

  $_YL_UPLOAD;

  $yl_filesize = $this ->

  yl_getfilename('size');

  if($yl_filesize ==

  0){

  $this ->

  alert("请选择上传文件!");

  終了;

  }

  if($yl_filesize

  > $_YL_UPLOAD['yl_maxsize']){

  スイッチ

  (strto lower($_YL_UPLOAD['yl_sizeformat'])){

  ケース

  'b':

  $yl_maxsizek = $_YL_UPLOAD['yl_maxsize'] .

  'B';

  休憩;

  ケース

  'k':

  $yl_maxsizek =

  $_YL_UPLOAD['yl_maxsize']/1024 。 '

  K';

  休憩;

  ケース

  'm':

  $yl_maxsizek =

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

  め;

  }

  $this ->

  alert("上传文件超出制限范围[".$yl_maxsizek."].K!");

  終了;

  }

  戻る

$yl_filesize;

}

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

#ファイル拡張子を取得

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

関数

yl_getfiletype()

{

グローバル

$_YL_UPLOAD;

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

$yl_file_ext =

strto lower($pathinfo['extension']);

// 拡張機能をチェック

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

{

$this ->

alert("アップロードされたファイルの種類は制限されています!");

終了;

}

戻る

$yl_file_ext;

}

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

#アップロード検証

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

関数

yl_upfile($source, $target) {

//

1 つの関数のアップロードに失敗した場合は、他の関数を使用してアップロードすることもできます

if (function_exists('move_uploaded_file')

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

{

@chmod($target, 0666);

戻る

$target;

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

{

@chmod($target, 0666);

戻る

$target;

elseif (@is_readable($source))

{

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

{

@flock($fp,2);

$filedata

=

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

@fclose($fp);

}

もし

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

@flock($fp,

2);

@fwrite($fp,

$filedata);

@fclose($fp);

@chmod

($target, 0666);

戻る

$target;

} else {

戻る

false;

}

}

}

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

#アップロード

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

関数

yl_uploadfile()

{

グローバル $_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_filenamenewst = $yl_filenamenews.'.'.$yl_file_type;//名前を変更

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

$yl_filenamenewst = $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">メール宛先:!@is_uploaded_file($yl_tmp_name">!@is_uploaded_file($yl_tmp_name)) )

{

$this ->

alert("ファイルがアップロードされました!");

終了;

}

// ディレクトリが存在するかどうかを確認し、存在しない場合は作成します

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.'');// ディレクトリを作成します

}

  //检查目录写权制限

  もし

  (

  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("上传目录没有写权限!");

  終了;

  }

  $yl_path_name

  =

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

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

  if($yl_doUpload === false){

  $this -> alert("上传失败!");

  終了;

  }その他{

  //エコー

  '上传成功';

  //エコー

  '
';

  /*

  エコー

  '翻訳ファイル名:'.$yl_filename.'';

  エコー

  '
';

  エコー

  '新しい文件名及目录:'.$yl_file_path.'/'.$yl_filenamenewst;

  エコー

  '
';

  エコー

  '文件大小:'.$yl_file_size.'';

  エコー '
';

  echo '文件类型:'.$yl_file_type.'';

  */

  $_YL_UPLOAD['yl_filename'] = $yl_filename;

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

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

  }その他{

  $_YL_UPLOAD['yl_isthumb'] = 0;

  }

  }その他{

  $_YL_UPLOAD['yl_isimage'] = 0;

  }

  }

  戻る

  true;

  }

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

  #提案

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

  関数

  アラート($yl_msg)

  {

  エコー '';

  エコー '';

  エコー '';

  エコー '';

  エコー '';

  エコー '';

  echo ' <script></script>';

  エコー '';

  エコー '';

  終了;

  }

  }



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。