PHP 上传图片精简版

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>   
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <html>    
    <head>    
    <title>图片上传</title>     
    </head>    
<body>   
<?php    
  $uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');    
  $addtime=date("Ym",time());      
  $testdir="./upload/".$addtime."/";   
  if(file_exists($testdir)):   
  else:   
  mkdir($testdir,0777);   
  endif;
  $max_file_size=2097152;     //上传文件大小限制, 单位BYTE    
  $destination_folder=$testdir; //上传文件路径    
  $imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);    
  $imgpreviewsize=1/2;    //缩略图比例    
?> 
<form enctype="multipart/form-data" method="POST" name="upform">    
上传文件:    
<input name="upfile" type="file">    
<input type="submit" value="上传"><br>    
允许上传的文件类型为:<?=implode(',',$uptypes)?>    
</form>    
    <?php    
    if ($_SERVER['REQUEST_METHOD'] == 'POST')    
    {
        if (!is_uploaded_file($_FILES["upfile"]["tmp_name"]))    
        //是否存在文件    
        {
             echo "图片不存在!";    
             exit;    
        }
        $file = $_FILES["upfile"];    
        if($max_file_size < $file["size"])    
        //检查文件大小    
        {
            echo "文件太大!";    
            exit;    
        }
        if(!in_array($file["type"], $uptypes))    
        //检查文件类型    
        {    
            echo "文件类型不符!".$file["type"];    
            exit;    
        }
        if(!file_exists($destination_folder))    
        {
			mkdir($destination_folder);    
        }
        $filename=$file["tmp_name"];    
        $image_size=getimagesize($filename);    
        $pinfo=pathinfo($file["name"]);    
        $ftype=$pinfo['extension'];    
        $destination = $destination_folder.time().".".$ftype;    
        if (file_exists($destination) && $overwrite != true)    
        {    
            echo "同名文件已经存在了";    
            exit;    
        }    
        
        if(!move_uploaded_file ($filename, $destination))    
        {    
            echo "移动文件出错";    
            exit;    
        }
        $pinfo=pathinfo($destination);    
        $fname=$pinfo["basename"];    
        echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>";    
        echo " 宽度:".$image_size[0];    
        echo " 长度:".$image_size[1];    
        echo "<br> 大小:".$file["size"]." bytes";    
        if($imgpreview==1)    
        {
        echo "<br>图片预览:<br>";    
        echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);    
        echo " title=\"图片预览:\r文件名:".$destination."\r上传时间:\">";    
        }    
    }
    ?>    
</body>    
</html> 

效果图:


 

猜你喜欢

转载自onestopweb.iteye.com/blog/2427698