php文件上传图片并将信息存到数据库

php文件上传图片并将信息存到数据库:上传到已有的已知目录;

上传表单:MAX_FILE_SIZE:表单上限制文件上传大小,这个值小于upload_max_filesize。

<form enctype="multipart/form-data" action="?action=up" method="post">
	         <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
	选择图片: <input type="file" name="userfile" style="cursor: pointer;"/>
	         <input type="submit" value="上传" style="cursor: pointer;" />
</form>

上传到指定文件夹,以当前时间戳为名移动到指定文件夹。

//设置上传图片的类型
    $_files = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif');
//判断类型是否是数组里的一种
    if (is_array($_files)) {
	if (!in_array($_FILES['userfile']['type'],$_files)) {
	    alert_back('上传图片必须是jpg,png,gif中的一种!');
			}
		}

    //判断文件错误类型
	if ($_FILES['userfile']['error'] > 0) {
		switch ($_FILES['userfile']['error']) {
			case 1: alert_back('上传文件超过约定值1');//upload_max_filesize
				break;
			case 2: alert_back('上传文件超过约定值2'); //MAX_FILE_SIZE
				break;
			case 3: alert_back('部分文件被上传');
				break;
			case 4: alert_back('没有任何文件被上传!');
				break;
		    }
		exit();
	}
    //判断配置大小
	if ($_FILES['userfile']['size'] > 1000000) {
	    alert_back('上传的文件不得超过1M');
		}
// 获取文件扩展名 $_n[1]就是文件扩展名
    $_n = explode('.', $_FILES['userfile']['name']);
    // 文件整个地址:
    $_name = 'face/'.time().'.'.$_n[1];
    //移动文件
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
	if(!@move_uploaded_file($_FILES['userfile']['tmp_name'],$_name)) {
	    alert_back('目录已被管理员删除');
        	} else {		
		//上传成功 修改face  value值
	echo "<script>alert('更换头像成功');window.opener.document.getElementById('img').src='$_name';window.opener.document.getElementById('face').value='$_name';window.close();</script>";
		exit();
	}
} else {
	alert_back('上传的临时文件不存在!');
}

修改数据库信息:

// 查询并显示数据
	if(isset($_COOKIE['username'])){
		if(!!$_rows = fetch_array("SELECT 
											id,
											username,
											password,
											sex,
											tag,
											face,
											date,
											email,
											qq,
											login_count,
											reg_time,
											level 
									FROM 
											study_user 
									WHERE 
											username='{$_COOKIE['username']}'
									")){
			$_html = array();
			$_html['id'] = $_rows['id'];
			$_html['tag'] = $_rows['tag'];
			$_html['username'] = $_rows['username'];
			$_html['password'] = $_rows['password'];
			$_html['sex'] = $_rows['sex'];
			$_html['face'] = $_rows['face'];
			if(!file_exists($_html['face'])){
				$_html['face'] = 'face/index.gif';
			}
			$_html['date'] = $_rows['date'];
			$_html['email'] = $_rows['email'];
			$_html['qq'] = $_rows['qq'];
			$_html['login_count'] = $_rows['login_count'];
			$_html['reg_time'] = $_rows['reg_time'];
			$_html['level'] = $_rows['level'];
				if($_html['level']==0){
					$_html['level'] = '普通用户';
				}else{
					$_html['level'] = '管理员';
				}
			$_html = html($_html);
		}
	}else{
		location(null,'login.php');
	}
	// 修改数据
	if(isset($_GET['action'])&&$_GET['action']=='modify'){
		$_clean = array();
		//引入验证文件
		// 如果不修改密码 密码等于上次的
		if(empty($_POST['password'])){
			$_clean['password'] = $_POST['pass'];
		}else{
			$_clean['password'] = sha1($_POST['password']);
		}
		// 如果不修改头像 头像等于上次的
		if(empty($_POST['face'])){
			$_clean['face'] = $_POST['faced'];
		}else{
			$_clean['face'] = $_POST['face'];
		}
		$_clean['sex'] = $_POST['sex'];
		$_clean['date'] = $_POST['date'];
		$_clean['email'] = $_POST['email'];
		$_clean['qq'] = $_POST['qq'];
		$_clean['tag'] = $_POST['tag'];
		$_clean = mysql_string($_clean);
		query("UPDATE 
						study_user 
				SET 	
						password = '{$_clean['password']}',
						face = '{$_clean['face']}',
						sex = '{$_clean['sex']}',
						date = '{$_clean['date']}',
						email = '{$_clean['email']}',
						qq = '{$_clean['qq']}',
						tag = '{$_clean['tag']}'
				WHERE 
					username = '{$_COOKIE['username']}'
				LIMIT 
					1");
		if(affected_rows()){
			location('资料更新成功','member.php');
		}else{
			alert_back('资料更新失败');
		}

	}

猜你喜欢

转载自blog.csdn.net/qq_41179401/article/details/81219336