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 addslashes 函数详细分析说明
Jun 23 PHP
解析阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql
Jun 26 PHP
Laravel 5 框架入门(二)构建 Pages 的管理功能
Apr 09 PHP
PHP yii实现model添加默认值的方法(两种方法)
Nov 10 PHP
Zend Framework分发器用法示例
Dec 11 PHP
使用一个for循环将N*N的二维数组的所有值置1实现方法
May 29 PHP
利用Homestead快速运行一个Laravel项目的方法详解
Nov 14 PHP
PHP长连接实现与使用方法详解
Feb 11 PHP
PHP下用Swoole实现Actor并发模型的方法
Jun 12 PHP
Laravel项目中timeAgo字段语言转换的改善方法示例
Sep 16 PHP
laravel框架 api自定义全局异常处理方法
Oct 11 PHP
Laravel 框架返回状态拦截代码
Oct 18 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 单引号与双引号的区别
2009/11/24 PHP
ezSQL PHP数据库操作类库
2010/05/16 PHP
PHP 断点续传实例详解
2017/11/11 PHP
浅谈PHP中pack、unpack的详细用法
2018/03/12 PHP
PHP匿名函数(闭包函数)详解
2019/03/22 PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
2020/08/17 PHP
document 和 document.all 分别什么时候用
2006/06/22 Javascript
JS Map 和 List 的简单实现代码
2013/07/08 Javascript
Mac/Windows下如何安装Node.js
2013/11/22 Javascript
JavaScript+CSS控制打印格式示例介绍
2014/01/07 Javascript
jquery 新建的元素事件绑定问题解决方案
2014/06/12 Javascript
轻松创建nodejs服务器(8):非阻塞是如何实现的
2014/12/18 NodeJs
jQuery元素的隐藏与显示实例
2015/01/20 Javascript
微信小程序之选项卡的实现方法
2017/09/29 Javascript
原生JavaScript实现拖动校验功能
2020/09/29 Javascript
[53:03]Optic vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python写的ARP攻击代码实例
2014/06/04 Python
Python过滤函数filter()使用自定义函数过滤序列实例
2014/08/26 Python
Python threading多线程编程实例
2014/09/18 Python
在Python中使用gRPC的方法示例
2018/08/08 Python
Python安装Flask环境及简单应用示例
2019/05/03 Python
python发送多人邮件没有展示收件人问题的解决方法
2019/06/21 Python
python print出共轭复数的方法详解
2019/06/25 Python
django的403/404/500错误自定义页面的配置方式
2020/05/21 Python
python 代码实现k-means聚类分析的思路(不使用现成聚类库)
2020/06/01 Python
Python类的继承super相关原理解析
2020/10/22 Python
英国床垫在线:Mattress Online
2016/12/07 全球购物
Antler英国官网:购买安特丽行李箱、拉杆箱
2019/08/25 全球购物
数字天堂软件测试面试题
2012/12/23 面试题
在校生自我鉴定
2014/01/23 职场文书
优秀教师工作感言
2014/02/16 职场文书
社区助残日活动总结
2014/08/29 职场文书
公司新人试用期自我评价
2014/09/17 职场文书
医生党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
vue实现可拖拽的dialog弹框
2021/05/13 Vue.js
一文带你理解vue创建一个后台管理系统流程(Vue+Element)
2021/05/18 Vue.js