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 相关文章推荐
mysql5写入和读出乱码解决
Nov 25 PHP
简单的PHP图片上传程序
Mar 27 PHP
PHP与SQL注入攻击防范小技巧
Sep 16 PHP
解析file_get_contents模仿浏览器头(user_agent)获取数据
Jun 27 PHP
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
Jul 03 PHP
回帖脱衣服的图片实现代码
Feb 15 PHP
PHP魔术引号所带来的安全问题分析
Jul 15 PHP
PHP输出缓冲控制Output Control系列函数详解
Jul 02 PHP
PHP记录页面停留时间的方法
Mar 30 PHP
php网页版聊天软件实现代码
Aug 12 PHP
PHP版微信小店接口开发实例
Nov 12 PHP
PHP实现提高SESSION响应速度的几种方法详解
Aug 09 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
《魔兽争霸3:重制版》更新 多项视觉效果调整
2020/05/04 魔兽争霸
基于PHP 面向对象之成员方法详解
2013/05/04 PHP
php定时计划任务的实现方法详解
2013/06/06 PHP
php 批量替换程序的具体实现代码
2013/10/04 PHP
PHP实现微信JS-SDK接口选择相册及拍照并上传的方法
2016/12/05 PHP
js操作ajax返回的json的注意问题!
2010/02/23 Javascript
javascript时区函数介绍
2012/09/14 Javascript
javascript使用onclick事件改变选中行的颜色
2013/12/30 Javascript
JQuery boxy插件在IE中边角图片不显示问题的解决
2015/05/20 Javascript
JavaScript实现自动弹出窗口并自动关闭窗口的方法
2015/08/06 Javascript
《JavaScript函数式编程》读后感
2015/08/07 Javascript
js右下角弹出提示框示例代码
2016/01/12 Javascript
JavaScript实现显示函数调用堆栈的方法
2016/04/21 Javascript
js获取时间函数及扩展函数的方法
2016/10/30 Javascript
微信小程序教程之本地图片上传(leancloud)实例详解
2016/11/16 Javascript
详谈jQuery中使用attr(), prop(), val()获取value的异同
2017/04/25 jQuery
npm国内镜像 安装失败的几种解决方案
2017/06/04 Javascript
深入浅析JavaScript中的RegExp对象
2017/09/18 Javascript
详解如何构建一个Angular6的第三方npm包
2018/09/07 Javascript
微信小程序开发之路由切换页面重定向问题
2018/09/18 Javascript
Vue常见面试题整理【值得收藏】
2018/09/20 Javascript
代码整洁之道(重构)
2018/10/25 Javascript
JS实现处理时间,年月日,星期的公共方法示例
2019/05/31 Javascript
Python获取当前时间的方法
2014/01/14 Python
python中split方法用法分析
2015/04/17 Python
浅析python3中的os.path.dirname(__file__)的使用
2018/08/30 Python
python多环境切换及pyenv使用过程详解
2019/09/27 Python
python3 使用Opencv打开USB摄像头,配置1080P分辨率的操作
2019/12/11 Python
HTML5实现视频直播功能思路详解
2017/11/16 HTML / CSS
以设计师精品品质提供快速时尚:Mostata
2019/05/10 全球购物
软件工程师岗位职责
2013/11/16 职场文书
餐饮业员工工作决心书
2014/03/11 职场文书
书香校园建设方案
2014/05/02 职场文书
端午节演讲稿
2014/05/23 职场文书
2014年仓库管理工作总结
2014/12/17 职场文书
2015年财政局工作总结
2015/05/21 职场文书