PHP之图片上传类实例代码(加了缩略图)


Posted in PHP onJune 30, 2016

有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file" name="file" /><input type="submit" name='submit' value="提交" ></form> 
 
<?php 
/** 
 * Created by PhpStorm. 
 * User: Administrator 
 * Date: 2016/6/28 
 * Time: 21:04 
 */
 
class upload{ 
   protected $fileMine;//文件上传类型 
   protected $filepath;//文件上传路径 
   protected $filemax;//文件上传大小 
   protected $fileExt;//文件上传格式 
   protected $filename;//文件名 
   protected $fileerror;//文件出错设置 
   protected $fileflag;//文件检测 
   protected $fileinfo; //FILES 
   protected $ext; //文件扩展 
   protected $path; 
 
  //文件上传 
  public function __construct($filename="file",$filemax=20000000,$filepath="./Uploads",$fileflag=true,$fileExt=array('jpg','exe'),$fileMine=array('image/jpeg')) 
  { 
    $this->filename=$filename; 
    $this->fileinfo=$_FILES[$this->filename]; 
    $this->filemax=$filemax; 
    $this->filepath=$filepath; 
    $this->fileflag=$fileflag; 
    $this->fileExt=$fileExt; 
    $this->fileMine=$fileMine; 
 
    //var_dump($this->filename); 
 
  } 
 
  //错误判断 
  public function UpError(){ 
 
      if($this->fileinfo['error']>0){ 
        switch($this->fileinfo['error']) 
        { 
          case 1: 
          $this->fileerror="上传文件大小超过服务器允许上传的最大值,php.ini中设置upload_max_filesize选项限制的值 "; 
            break; 
          case 2: 
            $this->fileerror="上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值"; 
            break; 
          case 3: 
            $this->fileerror="文件部分被上传"; 
            break; 
          case 4: 
            $this->fileerror="没有选择上传文件"; 
            break; 
          case 5: 
            $this->fileerror="未找到临时目录"; 
            break; 
          case 6: 
            $this->fileerror="文件写入失败"; 
            break; 
          case 7: 
            $this->fileerror="php文件上传扩展没有打开 "; 
            break; 
          case 8: 
            $this->fileerror=""; 
            break; 
 
        } 
        return false; 
      } 
      return true; 
 
  } 
 
  //检测文件类型 
  public function UpMine(){ 
    if(!in_array($this->fileinfo['type'],$this->fileMine)) { 
      $this->error="文件上传类型不对"; 
      return false; 
    } 
    return true; 
 
  } 
  //检测文件格式 
  public function UpExt(){ 
    $this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION); 
    //var_dump($ext); 
    if(!in_array($this->ext,$this->fileExt)){ 
      $this->fileerror="文件格式不对"; 
      return false; 
    } 
    return true; 
  } 
  //检测文件路径 
  public function UpPath(){ 
    if(!file_exists($this->filepath)){ 
      mkdir($this->filepath,0777,true); 
    } 
  } 
  //检测文件大小 
  public function UpSize(){ 
    $max=$this->fileinfo['size']; 
    if($max>$this->filemax){ 
      $this->fileerror="文件过大"; 
      return false; 
    } 
    return true; 
  } 
  //检测文件是否HTTP 
  public function UpPost(){ 
    if(!is_uploaded_file($this->fileinfo['tmp_name'])){ 
      $this->fileerror="恶意上偿还"; 
      return false; 
    } 
    return true; 
  } 
  //文件名防止重复 
  public function Upname(){ 
    return md5(uniqid(microtime(true),true)); 
  } 
 
  //图片缩略图 
  public function Smallimg($x=100,$y=100){ 
    $imgAtt=getimagesize($this->path); 
    //图像宽,高,类型 
    $imgWidth=$imgAtt[0]; 
    $imgHeight=$imgAtt[1]; 
    $imgext=$imgAtt[2]; 
    //等比列缩放 
 
    if(($x/$imgWidth)>($y/$imgHeight)){ 
      $bl=$y/$imgHeight; 
    }else{ 
      $bl=$x/$imgWidth; 
    } 
    $x=floor($imgWidth*$bl); //缩放后 
    $y=floor($imgHeight*$bl); 
    $images=imagecreatetruecolor($x,$y); 
    $big=imagecreatefromjpeg($this->path); 
    imagecopyresized($images,$big,0,0,0,0,$x,$y,$imgWidth,$imgWidth); 
    switch($imgext){ 
      case 1: 
        $imageout=imagecreatefromgif($this->path); 
        break; 
      case 2: 
        $imageout=imagecreatefromjpeg($this->path); 
        break; 
      case 3: 
        $imageout=imagecreatefromgif($this->path); 
        break; 
    } 
    $im=imagejpeg($images,$this->path); 
 
 
 
 
  } 
 
  //文件双传 
  public function uploads() 
  { 
    if($this->UpError()&&$this->UpMine()&&$this->UpExt()&&$this->UpSize()&&$this->UpPost()){ 
      $this->UpPath(); 
      $names=$this->Upname(); 
      $this->path=$this->filepath.'/'. $names.'.'.$this->ext; 
 
      if(move_uploaded_file($this->fileinfo['tmp_name'], $this->path)){ 
        return $this->path; 
      }else{ 
        $this->fileerror="上传失败"; 
      } 
    }else{ 
      exit("<b>".$this->fileerror."</b>"); 
    } 
  } 
 
 
} 
 
 
?>
<?php 
  header("content-type:imagejpeg"); 
header("Content-type:text/html;charset=utf-8"); 
 require 'list.php'; 
 $u=new upload(); 
 $a=$u->uploads(); 
 
 $c=$u->Smallimg(); 
echo "<img src={$a} />"; 
echo "<img src={$c} />"; 
 
?>
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<title>Examples</title> 
<meta name="description" content=""> 
<meta name="keywords" content=""> 
<link href="" rel="stylesheet"> 
</head> 
<body> 
  <form action="ce.php" enctype="multipart/form-data" method="post" > 
  <input type="text" name="name" /><input type="file" name="file" /> 
  <input type="submit" name='submit' value="提交" > 
  </form> 
</body> 
</html>

以上这篇PHP之图片上传类实例代码(加了缩略图)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP面向接口编程 耦合设计模式 简单范例
Mar 23 PHP
PHP服务器页面间跳转实现方法
Aug 02 PHP
destoon实现底部添加你是第几位访问者的方法
Jul 15 PHP
phpstorm配置Xdebug进行调试PHP教程
Dec 01 PHP
php线性表的入栈与出栈实例分析
Jun 12 PHP
PHP数组函数array_multisort()用法实例分析
Apr 02 PHP
CI框架常用经典操作类总结(路由,伪静态,分页,session,验证码等)
Nov 21 PHP
php实现微信模拟登陆、获取用户列表及群发消息功能示例
Jun 28 PHP
thinkPHP利用ajax异步上传图片并显示、删除的示例
Sep 26 PHP
php字符串截取函数mb_substr用法实例分析
Jun 25 PHP
thinkphp5实现微信扫码支付
Dec 23 PHP
PHP实现基本留言板功能原理与步骤详解
Mar 26 PHP
windows server 2008/2012安装php iis7 mysql环境搭建教程
Jun 30 #PHP
Yii2中使用join、joinwith多表关联查询
Jun 30 #PHP
Yii2 ActiveRecord多表关联及多表关联搜索的实现
Jun 30 #PHP
eclipse php wamp配置教程
Jun 30 #PHP
PHP上传图片类显示缩略图功能
Jun 30 #PHP
PHP使用php-resque库配合Redis实现MQ消息队列的教程
Jun 29 #PHP
Thinkphp批量更新数据的方法汇总
Jun 29 #PHP
You might like
PHP数据缓存技术
2007/02/14 PHP
PHP HTTP 认证实例详解
2016/11/03 PHP
Yii2中简单的场景使用介绍
2017/06/02 PHP
Laravel 6 将新增为指定队列任务设置中间件的功能
2019/08/06 PHP
浅说js变量
2011/05/25 Javascript
jquery绑定原理 简单解析与实现代码分享
2011/09/06 Javascript
Jquery异步请求数据实例代码
2011/12/28 Javascript
JS实现时间格式化的方式汇总
2013/10/16 Javascript
jQuery标签替换函数replaceWith()的使用例子
2014/08/28 Javascript
js实现按钮加背景图片常用方法
2014/11/01 Javascript
jQuery中prevUntil()方法用法实例
2015/01/08 Javascript
基于jquery步骤进度条源码分享
2015/11/12 Javascript
js+ajax实现获取文件大小的方法
2015/12/08 Javascript
JS代码随机生成姓名、手机号、身份证号、银行卡号
2016/04/27 Javascript
详解Nodejs之静态资源处理
2017/06/05 NodeJs
Vue2.0生命周期的理解
2018/08/20 Javascript
vue使用websocket的方法实例分析
2019/06/22 Javascript
vuex根据不同的用户权限展示不同的路由列表功能
2019/09/20 Javascript
nodejs各种姿势断点调试的方法
2020/06/18 NodeJs
Python BeautifulSoup中文乱码问题的2种解决方法
2014/04/22 Python
基于python的七种经典排序算法(推荐)
2016/12/08 Python
Python表示矩阵的方法分析
2017/05/26 Python
如何在Python中实现goto语句的方法
2019/05/18 Python
python实现引用其他路径包里面的模块
2020/03/09 Python
Python unittest基本使用方法代码实例
2020/06/29 Python
Python通过类的组合模拟街道红绿灯
2020/09/16 Python
html5如何及时更新缓存文件(js、css或图片)
2013/06/24 HTML / CSS
SVG实现多彩圆环倒计时效果的示例代码
2017/11/21 HTML / CSS
美国内衣品牌:Leonisa
2016/08/14 全球购物
超30万乐谱下载:Musicnotes.com
2016/09/24 全球购物
预订旅游活动、景点和旅游:GetYourGuide
2019/09/29 全球购物
3.15国际消费者权益日主题活动活动总结
2014/03/16 职场文书
小学生国庆65周年演讲稿范文(2篇)
2014/09/21 职场文书
孕妇离婚协议书范本
2014/11/20 职场文书
2015年小学中秋节活动总结
2015/03/23 职场文书
单位领导婚礼致辞
2015/07/28 职场文书