• 技术文章 >php教程 >PHP源码

    实例简单php图片上传类

    2016-06-08 17:28:08原创536

    class Uploadimg{
    private $_fileName=""; //文件域名称 如 'userfile'
    private $_uploadDir = ''; //上传路径 如 ./upload/

    /*上传参数配置*/
    private $_config = array(
    'type'=>array('image/jpeg','image/jpg',
    'image/pjpeg','image/gif'), //上传的类型
    'size'=>1, //文件最大容量单位是M
    'width'=>1000, //图片的最大宽度
    'height'=>800 //图片的最大高度
    );

    /**
    * 构造函数
    *
    * @param string $fileName
    * @param string $uploadDir
    * @param array $config
    */
    function __construct($fileName,$uploadDir,$config='')
    {
    $this->_fileName = $fileName;
    $this->_uploadDir = $uploadDir;
    if($config == "" or empty($config) or !is_array($config)){
    $this->_config = $this->_config;
    }else{
    $this->_config = $config;
    }
    }

    /**
    * 检测容量是否超过
    *
    * @return boolean
    */
    function checkSize()
    {
    if($_FILES[$this->_fileName]['size'] > $this->_config['size']*1024*1024){
    return false;
    }else{
    return true;
    }
    }

    /**
    * 获取图片信息
    *
    * @return boolean
    */
    function getInfo()
    {
    return @getimagesize($_FILES[$this->_fileName]['tmp_name']);
    }

    /**
    * 检测后缀名
    *
    * @return boolean
    */
    function checkExt()
    {
    $imageInfo = $this->getInfo();
    if(in_array($imageInfo['mime'],$this->_config['type'])){
    return true;
    }else{
    return false;
    }
    }

    /**
    * 获取后缀名
    *
    * @return boolean
    */
    function getExt()
    {
    $imageInfo = $this->getInfo();
    switch($imageInfo['mime']){
    case 'image/jpeg':$filenameExt = '.jpg';break;
    case 'image/jpg':$filenameExt = '.jpg';break;
    case 'image/pjpeg':$filenameExt = '.jpg';break;
    case 'image/gif':$filenameExt = '.gif';break;
    default:break;
    }
    return $filenameExt;
    }

    /**
    * 检测尺寸
    *
    * @return boolean
    */
    function checkWh()
    {
    $imageInfo = $this->getInfo();
    if(($imageInfo[0] > $this->_config['width']) or ($imageInfo[1] > $this->_config['height'])){
    return false;
    }else{
    return true;
    }
    }

    /**
    * 上传一张图片
    *
    * @return string or int
    */
    function uploadSingleImage()
    {
    if($this->checkSize() == false){
    return (-3); /*上传容量过大*/
    exit();
    }
    if($this->checkExt() == true){
    $filenameExt = $this->getExt();
    }else{
    return (-2); /*上传格式错误*/
    exit();
    }
    if($this->checkWh() == false){
    return (-1); /*上传图片太宽或太高*/
    exit();
    }
    $file_new_name = date('YmdHis').$filenameExt;
    $file_new_name_upload = rtrim($_SERVER['DOCUMENT_ROOT'],'//m.sbmmt.com/m/').$this->_uploadDir.$file_new_name;
    if(@move_uploaded_file($_FILES[$this->_fileName]['tmp_name'],$file_new_name_upload)){
    return $file_new_name;
    }else{
    return (0); /*上传失败*/
    }
    }

    /**
    * 删除图片
    *
    * @param string $imageName
    * @return boolen
    */
    function delImage($imageName)
    {
    $path = rtrim($_SERVER['DOCUMENT_ROOT'],'//m.sbmmt.com/m/').$this->_uploadDir.$imageName;
    if(unlink($path) == true){
    return true;
    }else{
    return false;
    }
    }
    }

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:nbsp return this config gt
    上一篇:google PR劫持查询程序 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• PHP实现301跳转,及延时跳转代码• php入门教程-留言板程序• PHP中上传多个文件到服务器实例• php 列出目录与删除目录实例代码• php 模拟用户抓取网页内容程序
    1/1

    PHP中文网