www.gusucode.com > 图片存储_淘宝相册_免费网盘源代码 > update.php

    <?php
// 注意:使用组件上传,不可以使用 $_FILES["Filedata"]["type"] 来判断文件类型
session_start();

if(file_exists("install.php")) { header('Location: install.php'); }
include "mysql_data.db.php";

mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb);

include "functions.php";
include "user.php";
//include "upload.php";
//$imgfunction = new uploads();
$globalvars->set_vars();
$user_dat = $user->process_user($globalvars);
//$template->header($globalvars);


mb_http_input("utf-8");
mb_http_output("utf-8");
$type=filekzm($_FILES["imgfile"]["name"]);
if ((($type == ".gif")
|| ($type == ".png")
|| ($type == ".jpeg")
|| ($type == ".jpg")
|| ($type == ".gif")
|| ($type == ".bmp"))
&& ($_FILES["Filedata"]["size"] < 20000000))
  {
  if ($_FILES["Filedata"]["error"] > 0)
    {
    echo "返回错误: " . $_FILES["imgfile"]["error"] . "<br />";
    }
  else
    {

    if (file_exists( $_FILES["imgfile"]["name"]))
      {
      echo $_FILES["imgfile"]["name"] . " already exists. ";
      }
    else
      {
	   upload_transfer($user_dat, $globalvars, 'imgfile');
      }
    }
  }
else
  {
  echo "上传失败,请检查文件类型和文件大小是否符合标准<br />文件类型:".$type.'<br />文件大小:'.($_FILES["Filedata"]["size"] / 1024) . " Kb";
  }
  
function filekzm($a)
{
	   // echo $a;
	$c=strrchr($a,'.');
	if($c)
	{
		return strtolower($c);
	}else{
		return '';
	}
}

function filekzm_water($a)
{
	   // echo $a;
	$c=strrchr($a,'.');
	$FileName =substr($a, 0, strrpos($a,'.'));
	if($c)
	{
		return $FileName."_s".$c;
	}else{
		return '';
	}
}
	//批量上传传输函数
	function upload_transfer($user_dat, $globalvars, $whichfile)
	{
		$error_occured = false;
		
		//echo "<i>" . $_FILES[$whichfile]['name'] . " 上传统计</i>:<br />";
		
		switch($_FILES[$whichfile]['error'])
		{
			case 1:
				$error_occured = true;
				echo "您试图上传的文件的大小超过系统限制.<br />";
			break;
				
			case 2:
				$error_occured = true;
				echo "您试图上传的文件的大小超过系统限制.<br />";
			break;
		}
				
		$split_ext = explode(',', $globalvars->settings['allowed_ext']);
		$split_img = explode('.', $_FILES[$whichfile]['name']);
		$extcount = count($split_ext);

		for($i=0;$i<$extcount;$i++)
		{
			$invalidimg = true;
			$split_values = explode(':', $split_ext[$i]);
			$i_values = $extcount - 1;
			
			if($split_values[0] == strtolower($split_img[1]) && $split_values[1] == "allow")
			{	
				$invalidimg = false;
				$i = $extcount + 1000;
			} elseif($i == $i_values && $split_values[0] != strtolower($split_img[1]))
			{
				// If the image was valid, we would have exited by now.
				$error_occured = true;
			}
		}
	
		if(is_dir($user_dat['usrdir']))
		{
			$dirsize_set = new functions();
			$dirsize = $dirsize_set->mb_bytes($user_dat['maxspace']);
			if($user_dat['usedspace'] < $dirsize)
			{
				if(file_exists($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))
				{
					if($_POST['overwrite_file'] == true)
					{
						unlink($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']);
					} else {
						$error_occured = true;
								
						//echo "您试图上传的文件已经存在<br />请选择覆盖或者更改文件名重新上传.<br /><br />";
						echo "您试图上传的文件:".$_FILES[$whichfile]['name']."已经存在<br />";
					}
				}
			} else {
				$error_occured = true;
				echo "您已经用光了所有的目录空间.";
			}
		} else {
			$error_occured = true;				
			echo "目录不存在. 请联系管理员.";
		}
				
		if($error_occured != true)
		{
			//防止中文名出错
			if (function_exists("iconv")) {
			  $_FILES[$whichfile]['name']= iconv("UTF-8","GB2312",$_FILES[$whichfile]['name']);
			 }
			if(move_uploaded_file($_FILES[$whichfile]['tmp_name'], $user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))
			{
				//生成随机数
				$randomnum=NULL;
				for($j=0;$j<=5;$j++)//随机数字的长度,本例随机数长度为6
				{
					srand((double)microtime()*1000000);
					$randomnumber=rand(!$j ? 1: 0,9);//产生随机数,不以0为第一个数,有些特殊的地方0开头被系统省略
					$randomnum.=$randomnumber;
				}
				//rename($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name'], $user_dat['usrdir'] . "/" . $split_img[0] . "." . strtolower($split_img[1]));
				//为文件改名
				$filename = time().'_'.$randomnum.  "." . strtolower($split_img[1]);
				rename($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name'], $user_dat['usrdir'] . "/".$filename);
				chmod($user_dat['usrdir'] . "/" . $filename, 0644);
				//计算图片大小
				$usrfile_size = filesize($user_dat['usrdir'] . "/" . $filename);
				//打水印
				imageWaterMark($user_dat['usrdir'] . "/".$filename,9,$waterImage); 
				//把上传图片插入数据库
				mysql_query("INSERT INTO imgup_imgs(picname,filename,filesize,userid) VALUES ('".$filename."','".$filename."',".$usrfile_size.",".$user_dat['id'].")");
				//echo "INSERT INTO imgup_imgs(picname,filename,userid) VALUES ('".$filename."','".$filename."',".$user_dat['id'].")";		
				echo "图片上传成功. ";
				echo "点击 " . '<a href="users/' . $user_dat['name'] . "/" . $filename . '" target="_blank">这里</a>' . " 查看图片.";
				//生成缩略图
				require_once("saveThumbnail.php");
				$thumbnail_make = new functions();
				$thumbnail_make->saveThumbnail($user_dat['usrdir'] . "/thumbnails/", $user_dat['usrdir'] . "/".$filename , $filename, 120, 40);
			} else {
				echo "<DIV align=center style=\"width: 500px; height:20px; text-align:left; color:#959595\"><b><font color=red>未知错误</b></div>";
			}
		} else {
			if($invalidimg == true)
			{
				echo "您试图上传的图片格式不在允许范围内.";
			}			
		}
		
	}


?>