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 相关文章推荐
图形数字验证代码
Oct 09 PHP
将文件夹压缩成zip文件的php代码
Dec 14 PHP
php函数的常用方法及注意之处小结
Jul 10 PHP
smarty基础之拼接字符串的详解
Jun 18 PHP
PHP中获取时间的下一周下个月的方法
Mar 18 PHP
PHP简单实现“相关文章推荐”功能的方法
Jul 19 PHP
PHP通过内置函数memory_get_usage()获取内存使用情况
Nov 20 PHP
thinkphp循环结构用法实例
Nov 24 PHP
CI框架给视图添加动态数据
Dec 01 PHP
为PHP5.4开启Zend OPCode缓存
Dec 26 PHP
golang与PHP输出excel示例
Jul 22 PHP
python进程与线程小结实例分析
Nov 11 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判断服务器是否支持Gzip压缩功能
2013/09/24 PHP
ThinkPHP使用心得分享-上传类UploadFile的使用
2014/05/15 PHP
PHP生成条形码大揭秘
2015/09/24 PHP
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
2016/01/09 PHP
Thinkphp单字母函数使用指南
2016/05/08 PHP
PHP实现移除数组中为空或为某值元素的方法
2017/01/07 PHP
PHP实现的mysql主从数据库状态检测功能示例
2017/07/20 PHP
Jquery+JSon 无刷新分页实现代码
2010/04/01 Javascript
ExtJs默认的字体大小改变的几种方法(自己整理)
2013/04/18 Javascript
我的Node.js学习之路(四)--单元测试
2014/07/06 Javascript
9款2014最热门jQuery实用特效推荐
2014/12/07 Javascript
Spring mvc 接收json对象
2015/12/10 Javascript
浅谈JavaScript前端开发的MVC结构与MVVM结构
2016/06/03 Javascript
详解vue 实例方法和数据
2017/10/23 Javascript
基于JavaScript实现瀑布流布局
2018/08/15 Javascript
Javascript读取上传文件内容/类型/字节数
2019/04/30 Javascript
vue 中使用 watch 出现了如下的报错的原因分析
2019/05/21 Javascript
vue组件命名和props命名代码详解
2019/09/01 Javascript
浅谈JavaScript中this的指向问题
2020/07/28 Javascript
[00:43]FTP典藏礼包 DOTA2三大英雄霸气新套装
2014/03/21 DOTA
[02:08]我的刀塔不可能这么可爱 胡晓桃_1
2014/06/20 DOTA
Python使用MYSQLDB实现从数据库中导出XML文件的方法
2015/05/11 Python
使用python实现tcp自动重连
2017/07/02 Python
python 环境变量和import模块导入方法(详解)
2017/07/11 Python
基于Django的python验证码(实例讲解)
2017/10/23 Python
python实现Floyd算法
2018/01/03 Python
JavaScript中的模拟事件和自定义事件实例分析
2018/07/27 Python
python实现按行分割文件
2019/07/22 Python
Pytorch中.new()的作用详解
2020/02/18 Python
Python图像处理二值化方法实例汇总
2020/07/24 Python
python time.strptime格式化实例详解
2021/02/03 Python
Farfetch阿联酋:奢侈品牌时尚购物平台
2019/07/26 全球购物
购买正版游戏和游戏激活码:Green Man Gaming
2019/11/06 全球购物
建筑工程质量通病防治方案
2014/06/08 职场文书
撤诉申请怎么写
2015/05/19 职场文书
python spilt()分隔字符串的实现示例
2021/05/21 Python