0 Comments

上传文件的服务器端处理

发布于:2012-11-19  |   作者:广州网站建设  |   已聚集:人围观
上传文件的服务器端处理

上传客户端文件(upload.html)中定义了getFileSize(filename)函数,它的作用是对上传的文件尺寸在客户端进行验证。由于客户端程序的各种限制,不能对上传文件做更多地判断和检测,这部分工作只能交给服务器端的PHP脚本文件来完成。广州网站设计

现在打开upload.php文件,完成类型判断、异常处理、信息提示、缩略图(仅限图片文件)等功能,核心代码如下:广州网站设计


  1. /*  
  2. *功能:上传文件检测程序  
  3. */  
  4. $file = &$HTTP_POST_FILES ['userfile'];  
  5. if ($max_file_size < $file ["size"]){           //检查文件大小  
  6.     echo "<font color='red'>文件太大!</font>";  
  7.     exit ();  
  8. }  
  9. if (! in_array ( $file ["type"], $uptypes ))    //检查文件类型  
  10. {  
  11.     echo "<font color='red'>不能上传此类型文件!</font>";  
  12.     exit ();  
  13. }  
  14. //检查上传目录是否存在,如果不存在则创建  
  15. if (! file_exists ( $destination_folder )) {  
  16.     mkdir ( $destination_folder );  
  17. }  
  18. $filename = $file ["tmp_name"];  
  19. $image_size = getimagesize ( $filename );  
  20. $pinfo = pathinfo ( $file ["name"] );  
  21. $ftype = $pinfo [extension];  
  22. $destination = $destination_folder . time () . "." . $ftype;  
  23. $fname = time () . "." . $ftype;  
  24. if (file_exists ( $destination ) && $overwrite != true) {  
  25.     echo "<font color='red'>同名文件已经存在了!</a>";  
  26.     exit ();  
  27. }  
  28. //移动文件异常处理  
  29. if (! move_uploaded_file ( $filename, $destination )) {  
  30.     echo "<font color='red'>移动文件出错!</a>";  
  31.     exit ();  
  32. } else {  
  33.     //添加写入数据库的部分  
  34.     //创建数据库连接  
  35.     $con = mysql_connect ( 'localhost', 'root', '198251' ) or die ( 'Could   
  36.     not connect: ' . mysql_error () );  
  37.     //echo 'Connected successfully';  
  38.     $db = mysql_select_db ( 'download', $con );  
  39.     if (! $db) {  
  40.         die ( "Can\'t use download : " . mysql_error () );  
  41.     } else {  
  42.         //将用户信息插入数据库的user表  
  43.         $sql = "INSERT INTO 'download'.'f_detail' ('id' ,'filename',  
  44.         'des' ,'fsize' ,'ftype' ,'utime' )VALUES (NULL ,'" . $fname . "' ,   
  45.         '', '" . $file ["size"] . "', '" . $file ["type"] . "',NOW());";  
  46.         echo $sql;  
  47.         $result = mysql_query ( $sql );  
  48.         if (! $result) {  
  49.             mysql_free_result ( $result );  //释放结果集  
  50.             mysql_close ( $db );            //关闭连接  
  51.             echo '数据记录插入失败!';  
  52.             exit ();  
  53.         }  
  54.     }  
  55. }  
  56. $pinfo = pathinfo ( $destination );  
  57. $fname = $pinfo [basename];  
  58. echo "<table><tr><td>上传文件地址:http://" . $_SERVER ['SERVER_NAME'] . $path_parts ["dirname"] . "/" . $destination_folder . $fname . "</td> </tr></table>";  
  59. echo " 宽度:" . $image_size [0];  
  60. echo " 长度:" . $image_size [1];  
  61. if ($watermark == 1) {  
  62.     $iinfo = getimagesize ( $destination, $iinfo );  
  63.     $nimage = imagecreatetruecolor ( $image_size [0], $image_size [1] );  
  64.     $white = imagecolorallocate ( $nimage, 255, 255, 255 );  
  65.     $black = imagecolorallocate ( $nimage, 0, 0, 0 );  
  66.     $red = imagecolorallocate ( $nimage, 255, 0, 0 );  
  67.     imagefill ( $nimage, 0, 0, $white );  
  68.     switch ($iinfo [2]) {  
  69.         case 1 :  
  70.             $simage = imagecreatefromgif ( $destination );  
  71.             break;  
  72.         case 2 :  
  73.             $simage = imagecreatefromjpeg ( $destination );  
  74.             break;  
  75.         case 3 :  
  76.             $simage = imagecreatefrompng ( $destination );  
  77.             break;  
  78.         case 6 :  
  79.             $simage = imagecreatefromwbmp ( $destination );  
  80.             break;  
  81.         default :  
  82.             die ( "<font color='red'>不能上传此类型文件!</a>" );  
  83.             exit ();  
  84.     }  
  85.     imagecopy ( $nimage, $simage, 0, 0, 0, 0, $image_size [0], $image_size   
  86.     [1] );  
  87.     imagefilledrectangle ( $nimage, 1, $image_size [1] - 15, 80, $image_size   
  88.     [1], $white );  
  89.     switch ($iinfo [2]) {  
  90.         case 1 :  
  91.             imagejpeg ( $nimage, $destination );  
  92.             break;  
  93.         case 2 :  
  94.             imagejpeg ( $nimage, $destination );  
  95.             break;  
  96.         case 3 :  
  97.             imagepng ( $nimage, $destination );  
  98.             break;  
  99.         case 6 :  
  100.             imagewbmp ( $nimage, $destination );  
  101.             break;  
  102.     }  
  103.     //覆盖原上传文件  
  104.     imagedestroy ( $nimage );  
  105.     imagedestroy ( $simage );  
  106. }  
  107. if ($imgpreview == 1) {  
  108.     echo "<br>图片预览:<br>";  
  109.     echo "<a href=\"" . $destination . "\" target='_blank'><img src=\"" .   
  110.     $destination . "\" width=" . ($image_size [0] * $imgpreviewsize) . "   
  111.     height=" . ($image_size [1] * $imgpreviewsize);  
  112.     echo " alt=\"图片预览:\r文件名:" . $fname . "\r上传时间:" . date ( 'm/d/Y   
  113.     h:i' ) . "\" border='0'></a>";  
  114. }  
  115. ?> 

 

修改后的upload.php程序可以完成将文件上传到指定的目录中的功能。当然,现在只是将文件上传到服务器的制定目录中去了,这样的文件凌乱且没有意义。广州网站建设
标签:
飞机