上传客户端文件(upload.html)中定义了getFileSize(filename)函数,它的作用是对上传的文件尺寸在客户端进行验证。由于客户端程序的各种限制,不能对上传文件做更多地判断和检测,这部分工作只能交给服务器端的PHP脚本文件来完成。广州网站设计
现在打开upload.php文件,完成类型判断、异常处理、信息提示、缩略图(仅限图片文件)等功能,核心代码如下:广州网站设计
- /*
- *功能:上传文件检测程序
- */
- $file = &$HTTP_POST_FILES ['userfile'];
- if ($max_file_size < $file ["size"]){ //检查文件大小
- echo "<font color='red'>文件太大!</font>";
- exit ();
- }
- if (! in_array ( $file ["type"], $uptypes )) //检查文件类型
- {
- echo "<font color='red'>不能上传此类型文件!</font>";
- 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;
- $fname = time () . "." . $ftype;
- if (file_exists ( $destination ) && $overwrite != true) {
- echo "<font color='red'>同名文件已经存在了!</a>";
- exit ();
- }
- //移动文件异常处理
- if (! move_uploaded_file ( $filename, $destination )) {
- echo "<font color='red'>移动文件出错!</a>";
- exit ();
- } else {
- //添加写入数据库的部分
- //创建数据库连接
- $con = mysql_connect ( 'localhost', 'root', '198251' ) or die ( 'Could
- not connect: ' . mysql_error () );
- //echo 'Connected successfully';
- $db = mysql_select_db ( 'download', $con );
- if (! $db) {
- die ( "Can\'t use download : " . mysql_error () );
- } else {
- //将用户信息插入数据库的user表
- $sql = "INSERT INTO 'download'.'f_detail' ('id' ,'filename',
- 'des' ,'fsize' ,'ftype' ,'utime' )VALUES (NULL ,'" . $fname . "' ,
- '', '" . $file ["size"] . "', '" . $file ["type"] . "',NOW());";
- echo $sql;
- $result = mysql_query ( $sql );
- if (! $result) {
- mysql_free_result ( $result ); //释放结果集
- mysql_close ( $db ); //关闭连接
- echo '数据记录插入失败!';
- exit ();
- }
- }
- }
- $pinfo = pathinfo ( $destination );
- $fname = $pinfo [basename];
- echo "<table><tr><td>上传文件地址:http://" . $_SERVER ['SERVER_NAME'] . $path_parts ["dirname"] . "/" . $destination_folder . $fname . "</td> </tr></table>";
- echo " 宽度:" . $image_size [0];
- echo " 长度:" . $image_size [1];
- if ($watermark == 1) {
- $iinfo = getimagesize ( $destination, $iinfo );
- $nimage = imagecreatetruecolor ( $image_size [0], $image_size [1] );
- $white = imagecolorallocate ( $nimage, 255, 255, 255 );
- $black = imagecolorallocate ( $nimage, 0, 0, 0 );
- $red = imagecolorallocate ( $nimage, 255, 0, 0 );
- imagefill ( $nimage, 0, 0, $white );
- switch ($iinfo [2]) {
- case 1 :
- $simage = imagecreatefromgif ( $destination );
- break;
- case 2 :
- $simage = imagecreatefromjpeg ( $destination );
- break;
- case 3 :
- $simage = imagecreatefrompng ( $destination );
- break;
- case 6 :
- $simage = imagecreatefromwbmp ( $destination );
- break;
- default :
- die ( "<font color='red'>不能上传此类型文件!</a>" );
- exit ();
- }
- imagecopy ( $nimage, $simage, 0, 0, 0, 0, $image_size [0], $image_size
- [1] );
- imagefilledrectangle ( $nimage, 1, $image_size [1] - 15, 80, $image_size
- [1], $white );
- switch ($iinfo [2]) {
- case 1 :
- imagejpeg ( $nimage, $destination );
- break;
- case 2 :
- imagejpeg ( $nimage, $destination );
- break;
- case 3 :
- imagepng ( $nimage, $destination );
- break;
- case 6 :
- imagewbmp ( $nimage, $destination );
- break;
- }
- //覆盖原上传文件
- imagedestroy ( $nimage );
- imagedestroy ( $simage );
- }
- if ($imgpreview == 1) {
- echo "<br>图片预览:<br>";
- echo "<a href=\"" . $destination . "\" target='_blank'><img src=\"" .
- $destination . "\" width=" . ($image_size [0] * $imgpreviewsize) . "
- height=" . ($image_size [1] * $imgpreviewsize);
- echo " alt=\"图片预览:\r文件名:" . $fname . "\r上传时间:" . date ( 'm/d/Y
- h:i' ) . "\" border='0'></a>";
- }
- ?>
修改后的upload.php程序可以完成将文件上传到指定的目录中的功能。当然,现在只是将文件上传到服务器的制定目录中去了,这样的文件凌乱且没有意义。广州网站建设



