0

0

PHP非常实用的上传类,上传效果在线演示

php中文网

php中文网

发布时间:2016-06-21 08:51:30

|

1116人浏览过

|

来源于php中文网

原创

 

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

  #文件名称:

  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;

  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)

  return '';

  $srcw = imagesx($im);

  $srch = imagesy($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("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

  && 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)];

  }

  }

  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 ' ';

  echo '';

  echo '';

  exit;

  }

  }



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
TensorFlow2深度学习模型实战与优化
TensorFlow2深度学习模型实战与优化

本专题面向 AI 与数据科学开发者,系统讲解 TensorFlow 2 框架下深度学习模型的构建、训练、调优与部署。内容包括神经网络基础、卷积神经网络、循环神经网络、优化算法及模型性能提升技巧。通过实战项目演示,帮助开发者掌握从模型设计到上线的完整流程。

0

2026.02.10

Vue3组合式API与组件开发实战
Vue3组合式API与组件开发实战

本专题讲解 Vue 3 组合式 API 的核心概念与应用技巧,深入分析响应式系统、生命周期管理、组件设计与复用策略。通过完整项目案例,指导前端开发者实现高性能、结构清晰的 Vue 应用,提升开发效率与代码可维护性。

2

2026.02.10

Go语言微服务架构与gRPC实战
Go语言微服务架构与gRPC实战

本专题面向有 Go 基础的开发者,系统讲解微服务架构设计与 gRPC 的高效应用。内容涵盖服务拆分、RPC 通信、负载均衡、错误处理、服务注册与发现等关键技术。通过实战案例,帮助开发者搭建高性能、可扩展的 Go 微服务系统。

1

2026.02.10

React 18状态管理与Hooks高级实践
React 18状态管理与Hooks高级实践

本专题专注于 React 18 的高级开发技术,详细讲解 useState、useEffect、useReducer、useContext 等 Hooks 的使用技巧,以及 Redux、Zustand 等状态管理工具的集成与优化方法。通过真实案例,帮助前端开发者构建可维护、性能优良的现代 React 应用。

3

2026.02.10

Node.js后端开发与Express框架实践
Node.js后端开发与Express框架实践

本专题针对初中级 Node.js 开发者,系统讲解如何使用 Express 框架搭建高性能后端服务。内容包括路由设计、中间件开发、数据库集成、API 安全与异常处理,以及 RESTful API 的设计与优化。通过实际项目演示,帮助开发者快速掌握 Node.js 后端开发流程。

0

2026.02.10

Java 并发编程与线程池实战
Java 并发编程与线程池实战

本专题面向中级 Java 开发者,深入讲解 Java 并发编程基础、线程创建、线程安全、锁机制及线程池使用。通过实战案例演示如何使用不同类型的线程池优化应用性能、管理并发任务,并结合高并发场景提供优化策略和最佳实践,帮助开发者提升 Java 并发处理能力。

0

2026.02.10

Golang处理数据库错误教程合集
Golang处理数据库错误教程合集

本专题整合了Golang数据库错误处理方法、技巧、管理策略相关内容,阅读专题下面的文章了解更多详细内容。

153

2026.02.06

java多线程方法汇总
java多线程方法汇总

本专题整合了java多线程面试题、实现函数、执行并发相关内容,阅读专题下面的文章了解更多详细内容。

91

2026.02.06

1688阿里巴巴货源平台入口与批发采购指南
1688阿里巴巴货源平台入口与批发采购指南

本专题整理了1688阿里巴巴批发进货平台的最新入口地址与在线采购指南,帮助用户快速找到官方网站入口,了解如何进行批发采购、货源选择以及厂家直销等功能,提升采购效率与平台使用体验。

877

2026.02.06

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
前端系列快速入门课程
前端系列快速入门课程

共4课时 | 0.4万人学习

Excel 教程
Excel 教程

共162课时 | 16.5万人学习

Pandas 教程
Pandas 教程

共15课时 | 1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号