【PHP】上传图像的处理

来源:互联网 发布:剑网3捏脸数据下载 编辑:程序博客网 时间:2024/05/16 00:45

<?
if(isset($_POST['send_image']))//当接到由表单发来的数据send_image时
 {
  if(is_uploaded_file($_FILES['file']['tmp_name']))//是否上传照片
   {
   list($width,$height, $type, $attr) =getimagesize($_FILES['file']['tmp_name']);//获取上传文件的属性
   if ( $type==1)//判断类型
   {
    $type=".gif";
   }
   else if($type==2 )
   {
    $type=".jpg";
   }
   else if($type==3 )
   {
    $type=".png";
   }
   else if($type==6 )
   {
    $type=".bmp";
   }
   else
   {
    echo"您只能上传以下类型图片:jpg,gif,bmp,png!";
   }
   
   if (($width>100) || ($height>100))//判断图像文件的长宽
   {
    echo"图片尺寸过大!";
   }
   else if ($_FILES['image']['size']>IMAGE_MAX_SIZE)//文件尺寸判断(大小)
   {
    echo"图片过大!";
   }
    else
    {
     do
     {
     $name=random(10).$type;
     if(!file_exists("./test/")){mkdir("./test/");}//判断储存图片的路径是否存在,如果不存在则创建一个
     $uploaddir= "./test/";
       $uploadfile=$uploaddir.$name;//最终的文件存储为$uploadfile(路径+文件名)
     }
     while(file_exists($uploadfile));
     if(is_uploaded_file($_FILES['file']['tmp_name']))
     {
       if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
       {
      echo"成功!";
       }
     }
    }
  }
  else
  {
   echo"您还没有选择图片文件!";
  }
 }
?>


<?

function random($length)//取得指定长度的随机字符串
  {
    $hash ='CR-';
    $chars ='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max =strlen($chars) - 1;
       for($i = 0; $i < $length; $i++)
       {
           $hash .= $chars[mt_rand(0, $max)];
       }
    return$hash;
  }

 

?>

0 0