PHP图片上传程序设计

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  2. <?php   
  3. //上传文件类型列表   
  4. $uptypes=array(   
  5.     'image/jpg',   
  6.     'image/jpeg',   
  7.     'image/png',   
  8.     'image/pjpeg',   
  9.     'image/gif',   
  10.     'image/bmp',   
  11.     'image/x-png'   
  12. );  
  13.   
  14. $max_file_size=2000000;     //上传文件大小限制, 单位BYTE   
  15. $destination_folder="uploadimg/"; //上传文件路径    
  16. $imgpreview=0;      //是否生成预览图(1为生成,其他为不生成);   
  17. $imgpreviewsize=1/1;    //缩略图比例   
  18. ?>   
  19.   
  20. <html>   
  21. <head>   
  22. <title>FileUpload</title>   
  23. <style type="text/css">   
  24. <!--   
  25. body   
  26. {   
  27.      font-size: 9pt;   
  28. }   
  29. input   
  30. {   
  31.      background-color: #66CCFF;   
  32.      border: 1px inset #CCCCCC;   
  33. }   
  34. -->   
  35. </style>   
  36. </head>  
  37.   
  38. <body>   
  39. <form enctype="multipart/form-data" method="post" name="upform">   
  40.   上传文件:   
  41.   <input name="upfile" type="file">   
  42.   <input type="submit" value="上传">  
  43. <?php   
  44. if ($_SERVER['REQUEST_METHOD'] == 'POST')   
  45. {   
  46.     if (!is_uploaded_file($_FILES["upfile"][tmp_name]))   
  47.     //是否存在文件   
  48.     {   
  49.          echo "图片不存在!";   
  50.          exit;   
  51.     }  
  52.   
  53.     $file = $_FILES["upfile"];   
  54.     if($max_file_size < $file["size"])   
  55.     //检查文件大小   
  56.     {   
  57.         echo "文件太大!";   
  58.         exit;   
  59.     }  
  60.   
  61.     if(!in_array($file["type"], $uptypes))   
  62.     //检查文件类型   
  63.     {   
  64.         echo "文件类型不符!".$file["type"];   
  65.         exit;   
  66.     }  
  67.   
  68.     if(!file_exists($destination_folder))   
  69.     {   
  70.         mkdir($destination_folder);   
  71.     }  
  72.   
  73.     $filename=$file["tmp_name"];   
  74.     $image_size = getimagesize($filename);   
  75.     $pinfo=pathinfo($file["name"]);   
  76.     $ftype=$pinfo['extension'];   
  77.     $destination = $destination_folder.time().".".$ftype;   
  78.     if (file_exists($destination) && $overwrite != true)   
  79.     {   
  80.         echo "同名文件已经存在了";   
  81.         exit;   
  82.     }  
  83.   
  84.     if(!move_uploaded_file ($filename, $destination))   
  85.     {   
  86.         echo "移动文件出错";   
  87.         exit;   
  88.     }  
  89.   
  90.     $pinfo=pathinfo($destination);   
  91.     $fname=$pinfo[basename];   
  92.     echo " <font color=red>已经成功上传</font><br>";   
  93.   
  94.     if($imgpreview==1)   
  95.     {   
  96.     echo "<br>图片预览:<br>";   
  97.     echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);   
  98.     echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";   
  99.     }   
  100. }   
  101. ?>  
  102.   <br>   
  103. </form>  
  104. </body>   
  105. </html>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值