PHP-文件上传

1.2 文件上传

1.2.1 封装文件上传类

1、在Lib目录下创建Upload.class.php

<?php 
namespace Lib;
class Upload{
	private $path;//上传的路径
	private $size;//上传的大小
	private $type;//允许上传的类型
	private $error;//保存错误信息
	
	public function __construct($path,$size,$type){
		$this->path=$path;
		$this->size=$size;
		$this->type=$type;
	}
	//返回错误信息
	public function getError(){
		return $this->error;
	}
	/**
	 * 文件上传
	 * @param $files_array $_FILES[]
	 * @ return bool|string 成功返回文件路径,失败返回false
	 */
	public function uploadOne($file){
		if($this->checkError($file)){ //没有错误就上传
			$foldername=date('Y-m-d');//文件夹名称
			$folderpath="./uploads/{$foldername}";//文件夹路径
			if(!is_dir($folderpath))
				mkdir($folderpath);
			$filename=uniqid('',true).strrchr($_FILES['face']['name'],'.');//文件名
			$filepath="$folderpath/$filename";//文件路径
			if(move_uploaded_file($_FILES['face']['tmp_name'],$filepath))
				return "{$foldername}/{$filename}";
			else{
				$this->error='上传失败<br>';
				return false;
			}
		}
		return false;
	}
	//验证上传是否有误
	private function checkError($file){
		//1:验证是否有误
		if($file['error']!=0){
			switch($file['error']){
				case 1:
					$this->error='文件大小超过了php.ini中允许的最大值,最大值是:'.ini_get('upload_max_filesize');
					return false;
				case 2:
					$this->error='文件大小超过了表单允许的最大值';
					return false;
				case 3:
					$this->error='只有部分文件上传';
					return false;
				case 4:
					$this->error='没有文件上传';
					return false;
				case 6:
					$this->error='找不到临时文件';
					return false;
				case 7:
					$this->error='文件写入失败';
					return false;
				default:
					$this->error='未知错误';
					return false;
			}
		}
		//2、验证格式
		$info=finfo_open(FILEINFO_MIME_TYPE);
		$mime=finfo_file($info,$file['tmp_name']);
		$allow=array('image/jpeg','image/png','image/gif');//允许的类别
		if(!in_array($mime,$allow)){
			$this->error='只能上传'.implode(',',$allow).'格式';
			return false;
		}
		//3、验证大小
		$size=1234;
		if($file['size']>$size){
			$this->error='文件大小不能超过'.number_format(($size/1024),1).'k';
			return false;
		}
		//4、验证是否是http上传
		if(!is_uploaded_file($file['tmp_name'])){
			$this->error='文件不是HTTP POST上传的<br>';
			return false;
		}
		return true;//没有错误
	}

}

2、更改注册控制器

//注册
	public function registerAction(){
		//第二步:执行注册逻辑
		if(!empty($_POST)){
			//文件上传
			$path=$GLOBALS['config']['app']['path'];
			$size=$GLOBALS['config']['app']['size'];
			$type=$GLOBALS['config']['app']['type'];
			$upload=new \Lib\Upload($path,$size,$type);
			if($path=$upload->uploadOne($_FILES['face'])){
				$data['user_face']=$path;
			}else{
				$this->error('index.php?p=Admin&c=Login&a=register',$upload->getError());
			}
			//文件上传结束
			...

3、配置文件

    'app'=>array(
		'path'=>'./Public/Uploads/',
		'size'=>1234567,
		'type'=>['image/png','image/jpeg','image/gif'],

4、register.html

<form action="" method="post" enctype="multipart/form-data">
...

1.2.2 封装缩略图类

在Lib目录下创建Image.class.php

<?php 
namespace Lib;
class Image(){
	/*
	*制作缩略图
	*@param $src_path 源图的路径
	*/
	public function thumb($src_path,$prefix='small_',$w=200,$h=200){
		$dst_img=imagecreatetruecolor($w,$h);//目标图
		$src_img=imagecreatefromjpeg($src_path);//源图
		$src_w=imagesx($src_img);
		$src_h=imagesy($src_img);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$w,$h,$src_w,$src_h);
		$filename=basename($src_path);//文件名
		$foldername=substr(dirname($src_path),-10);//目录名
		$dst_path="{$foldername}/{$prefix}{$filename}";
		$save_path=dirname($src_path).'/'.$prefix.$filename;
		imagejpeg($dst_img,$save_path);
		return "{$foldername}/{$prefix}{$filename}";
	}
}

等比例缩放

<?php 
$max_w=300;
$max_h=300;
$dst=imagecreatetruecolor($max_w,$max_h);
$src=imagecreatefromjpeg('./4.jpg');
$src_w=imagesx($src);//源图的宽度
$src_h=imagesy($src);//源图的高度
if($src_w/$src_h>$max_w/$max_h){源图宽/高比比需要缩放的宽/高大 按需要的宽走
	$dst_w=$max_w;
	$dst_h=$dst_w*$src_h/$src_w;
}else{//否则按需要的高走
	$dst_h=$max_h;
	$dst_w=$src_w*$dst_h/$src_h;
}
$dst_w=(int)$dst_w;
$dst_h=(int)$dst_h;
//目标的坐标
$dst_x=($max_w-$dst_w)/2;
$dst_y=($max_h-$dst_h)/2;
imagecopyresampled($dst,$src,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$src_w,$src_h);
header('content-type:image/jpeg');
imagejpeg($dst);

1.2.3 实现文件上传

LoginController.class.php文件

public function registerAction(){
		//第二步:执行注册逻辑
		if(!empty($_POST)){
			//文件上传
			$path=$GLOBALS['config']['app']['path'];
			$size=$GLOBALS['config']['app']['size'];
			$type=$GLOBALS['config']['app']['type'];
			$upload=new \Lib\Upload($path,$size,$type);
			if($path=$upload->uploadOne($_FILES['face'])){
				//生成缩略图
				$image=new \Lib\Imgae();
				$data['user_face']=$image->thumb($path,$filepath,'sl_');
				//$image->thumb($path,$filepath,'sl_');
			}else{
				$this->error('index.php?p=Admin&c=Login&a=register',$upload->getError());
			}
			//文件上传结束
			$data['user_name']=$_POST['username'];
			$data['user_pwd']=md5(md5($_POST['password'].$GLOHALS['config']['app']['key']));
			$model=new \Core\Model('user');
			if($model->insert($data))
				$this->success('index.php?p=Admin&c=Login&a=login','注册成功,您可以去登录了')
			else
				$this->error('index.php?p=Admin&c=Login&a=register','注册失败,请重新注册')
		}
		//第一步:显示注册界面
		require __VIEW__.'register.html';
	}

1.3 登录模块

1.3.1 记住密码

登录成功后,如果需要记住用户名和密码,则将用户名和密码记录在cookie中。
LoginController.class.php文件

if($info=$model->getUserByNameAndPwd($_POST['username'],$_POST['password'])){
				$_SESSION['user']=$info; //将用户信息保存到会话中
				$model->updateLoginInfo();//更新用户信息
				//记录用户名和密码
				if(isset($_POST['remember'])){
					$time=time()+3600*24*7;//记录7天
					setcookie('name',$_POST['username'],$time);
					setcookie('pwd',$_POST['password'],$time);
				}
				$this->success('index.php?p=Admin&c=Admin&a=admin','登录成功')

打开登录页面的时候,获取cookie的值
LoginController.class.php文件

//第一步:显示登录界面
		$name=$_COOKIE['name']??'';
		$pwd=$_COOKIE['pwd']??'';
		require __VIEW__.'login.html'

在视图页面(login.html)页面显示cookie的信息
打开页面自动显示登录信息

<input type="text" name="username" placeholder="登陆账号" value="<?=$name?>" />
...

<input type="password" name="password" placeholder="登录密码" value="<?=$pwd?>" />

1.3.2 安全退出

退出:退出的时候不消毁令牌
安全退出:退出的时候销毁了令牌
top.html

<a href="index.php?p=Admin&c=Login&a=logout" target="_top">安全退出</a>
_top表示在最顶端的窗口中打开

控制器(LoginController)

public function logoutAction(){
		session_destroy();
		header('location:index.php?p=Admin&c=Login&a=login');
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值