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实现的功能是显示8条基色色带
Oct 09 PHP
PHP中,文件上传
Dec 06 PHP
php 面向对象的一个例子
Apr 12 PHP
PHP递归返回值时出现的问题解决办法
Feb 19 PHP
php增删改查示例自己写的demo
Sep 04 PHP
PHP中Fatal error session_start()错误解决步骤
Aug 05 PHP
PHP实现HTML生成PDF文件的方法
Nov 07 PHP
php生成excel列名超过26列大于Z时的解决方法
Dec 29 PHP
php采集内容中带有图片地址的远程图片并保存的方法
Jan 03 PHP
Windows7下的php环境配置教程
Feb 28 PHP
PHP 极验验证码实例讲解
Sep 29 PHP
PHP实现基于状态的责任链审批模式详解
May 31 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/11/25 PHP
据说是雅虎的一份PHP面试题附答案
2009/01/07 PHP
PHP 时间转换Unix时间戳代码
2010/01/22 PHP
解析如何在PHP下载文件名中解决乱码的问题
2013/06/20 PHP
php顺序查找和二分查找示例
2014/03/27 PHP
PHP命名空间(namespace)的使用基础及示例
2014/08/18 PHP
一张表搞清楚php is_null、empty、isset的区别
2015/07/07 PHP
PHP常用日期加减计算方法实例小结
2018/07/31 PHP
PHP设计模式之模板模式定义与用法详解
2018/12/20 PHP
解决PHP使用CURL发送GET请求时传递参数的问题
2019/10/11 PHP
用正则获取指定路径文件的名称
2007/02/27 Javascript
Js实现网页键盘控制翻页的方法
2014/10/30 Javascript
nodejs实现获取某宝商品分类
2015/05/28 NodeJs
分享5个好用的javascript文件上传插件
2018/09/16 Javascript
Vue 自定义标签的src属性不能使用相对路径的解决
2019/09/17 Javascript
Nodejs封装类似express框架的路由实例详解
2020/01/05 NodeJs
Python中字符串的处理技巧分享
2016/09/17 Python
微信跳一跳游戏python脚本
2020/04/01 Python
对python程序内存泄漏调试的记录
2018/06/11 Python
python中sys.argv函数精简概括
2018/07/08 Python
Python3.5多进程原理与用法实例分析
2019/04/05 Python
Python爬取爱奇艺电影信息代码实例
2019/11/26 Python
HTML5 Canvas旋转动画的2个代码例子(一个旋转的太极图效果)
2014/04/10 HTML / CSS
html+js 实现markdown编辑器效果
2019/10/23 HTML / CSS
亚马逊印度站:Amazon.in
2017/10/15 全球购物
世界上最大的家庭自动化公司:Smarthome
2017/12/20 全球购物
Lookfantastic希腊官网:英国知名美妆购物网站
2018/09/15 全球购物
优秀员工自荐书
2013/12/19 职场文书
幼儿园小班评语大全
2014/04/17 职场文书
《记金华的双龙洞》教学反思
2014/04/19 职场文书
后进生评语大全
2015/01/04 职场文书
学校端午节活动总结
2015/02/11 职场文书
车间质检员岗位职责
2015/04/08 职场文书
2015-2016年小学教导工作总结
2015/07/21 职场文书
共青团优秀团员申请书(范文)
2019/08/15 职场文书
Grafana可视化监控系统结合SpringBoot使用
2022/04/19 Redis