zoukankan      html  css  js  c++  java
  • 简单图片上传

    protected function upload($f)
        {
            //全局变量
            $arrType=array('image/jpg','image/gif','image/png','image/bmp','image/pjpeg','image/jpeg');
            $max_size='500000';      // 最大文件限制(单位:byte)
            $upfile='./uploads'; //图片目录路径
            $file=$f['file'];
            if($_SERVER['REQUEST_METHOD']=='POST')
            {
            //判断提交方式是否为POST
                if(!is_uploaded_file($file['tmp_name'])){ //判断上传文件是否存在
                return "<font color='#FF0000'>文件不存在!</font>";
                exit;
                }
              
                  if($file['size']>$max_size)
                  {
                    //判断文件大小是否大于500000字节
                    return "<font color='#FF0000'>上传文件太大!</font>";
                    exit;
                   }
                  if(!in_array($file['type'],$arrType))
                  {
                    //判断图片文件的格式
                     return "<font color='#FF0000'>上传文件格式不对!</font>";
                     exit;
               }
                  if(!file_exists($upfile))
                  {
                    // 判断存放文件目录是否存在
                       mkdir($upfile,0777,true);
                   }
                      $imageSize=getimagesize($file['tmp_name']);
                       $img=$imageSize[0].'*'.$imageSize[1];
                       $fname=time().rand(1000,9999).substr($file['name'],strpos($file['name'],'.')) ;
                       $ftype=explode('.',$fname);
                       $picName=$upfile."/".time().$fname;
                  
                   if(file_exists($picName))
                   {
                    return "<font color='#FF0000'>同文件名已存在!</font>";
                    exit;
                 }
                   if(!move_uploaded_file($file['tmp_name'],$picName))
                   {
                    return "<font color='#FF0000'>移动文件出错!</font>";
                    exit;
                }
                   else
                   {
                    return $picName;
                }
            }
        }

  • 相关阅读:
    VS2012打开项目——已停止工作
    使用copydata实现进程之间数据传递
    WPF Demo20 模板
    WPF Demo19 命令、UC
    WPF Demo18 路由事件
    WPF Demo17 数据绑定
    Spark2.3.0 报 io.netty.buffer.PooledByteBufAllocator.metric
    Impala与Hive的优缺点和异同
    Hive 报错信息及解决方法
    hive表多种存储格式的文件大小差异,无重复数据
  • 原文地址:https://www.cnblogs.com/sensai-sun/p/6737238.html
Copyright © 2011-2022 走看看