php文件上传类

class Upload
{
//保存路径
protected $savePath = './';
//日期目录
protected $datePath = true;
//随机名字
protected $randName = true;
//默认后缀
protected $extension = 'png';
//允许MIME
protected $mimes = ['image/png','image/jpeg','image/gif'];
//允许后缀
protected $suffixes = ['png','jpg','jpeg','gif'];
//最大尺寸
protected $maxSize = 2000000;
//错误代码
protected $errorNumber = 0;
//错误信息
protected $errorMessage = '上传成功';
//上传信息
protected $uploadInfo;
//文件名称
protected $pathName;


public function __construct($options=null)
{
$this->setOption($options);
}


public function setOption($options)
{
if (is_array($options)) {
$keys = get_class_vars(__CLASS__);
foreach ($options as $key => $value) {
//在属性列表中才设置属性
if (in_array($key, $keys)) {
$this->$key = $value;
}
}
}
}


public function uploadFile($field)
{
//1、检查保存路径
if (!$this->checkSavePath()) {
return false;
}
//2、检查上传信息
if (!$this->checkUploadInfo($field)) {
return false;
}
//3、检查系统错误
if (!$this->checkUploadError()) {
return false;
}
//4、检查自定义错误
if (!$this->checkAllowOption()) {
return false;
}
//5、检车是否是上传文件
if (!$this->checkUploadFile()) {
return false;
}
//6、拼接路径名
$this->joinPathName();
//7、移动上传文件
if (!$this->moveUploadFile()) {
return false;
}
return true;
}


protected function checkSavePath()
{
if (!is_dir($this->savePath)) {
$this->errorNumber = -1;
$this->errorMessage = '保存路径不存在';
return false;
}
if (!is_writable($this->savePath)) {
$this->errorNumber = -2;
$this->errorMessage = '保存路径不可写';
return false;
}
$this->savePath = rtrim($this->savePath,'/') . '/';
return true;
}


protected function checkUploadInfo($field)
{
if (empty($_FILES[$field])) {
$this->errorNumber = -3;
$this->errorMessage = '没有'.$field.'相关上传信息';
return false;
}
$this->uploadInfo = $_FILES[$field];
return true;
}


protected function checkUploadError()
{
if ($this->uploadInfo['error'] == 0) {
return true;
}
switch ($this->uploadInfo['error']) {
case UPLOAD_ERR_INI_SIZE:
$this->errorMessage = '超出了配置文件中设定文件大小';
break;
case UPLOAD_ERR_FORM_SIZE:
$this->errorMessage = '超出了MAX_FILE_SIZE的大小';
break;
case UPLOAD_ERR_PARTIAL:
$this->errorMessage = '只有部分文件被上传';
break;
case UPLOAD_ERR_NO_FILE:
$this->errorMessage = '没有文件被上传';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$this->errorMessage = '没有找到临时文件夹';
break;
case UPLOAD_ERR_CANT_WRITE:
$this->errorMessage = '文件写入失败';
break;
default:
$this->errorMessage = '未知错误';
break;
}
$this->errorNumber = $this->uploadInfo['error'];
return false;
}


protected function checkAllowOption()
{
if (!in_array($this->uploadInfo['type'],$this->mimes)) {
$this->errorNumber = -4;
$this->errorMessage = '不允许的MIME:'.$this->uploadInfo['type'];
return false;
}
if (!in_array($this->extension,$this->suffixes)) {
$this->errorNumber = -5;
$this->errorMessage = '不允许的后缀:'.$this->extension;
return false;
}
if ($this->uploadInfo['size'] > $this->maxSize) {
$this->errorNumber = -6;
$this->errorMessage = '超出了规定大小:'.$this->maxSize.'字节';
return false;
}
return true;
}


protected function checkUploadFile()
{
if (!is_uploaded_file($this->uploadInfo['tmp_name'])) {
$this->errorNumber = -7;
$this->errorMessage = '不是上传文件';
return false;
}
return true;
}


protected function joinPathName()
{
//路径
$this->pathName = $this->savePath;
if ($this->datePath) {
$this->pathName .= date('Y/m/d/');
if (!file_exists($this->pathName)) {
mkdir($this->pathName,0777,true);
}
}
//名字
if ($this->randName) {
$this->pathName .= uniqid();
} else {
$info = pathinfo($this->uploadInfo['name']);
$this->pathName .= $info['filename'];
}
//后缀
$this->pathName .= '.' . $this->extension;
}


protected function moveUploadFile()
{
if (move_uploaded_file($this->uploadInfo['tmp_name'], $this->pathName)) {
return true;
}
$this->errorNumber = -8;
$this->errorMessage = '上传文件保存';
return false;
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件上传是网站开发中常用的功能之一,PHP文件上传可以帮助我们实现文件上传功能。当用户需要上传文件时,可以使用PHP文件上传来处理文件上传的逻辑。PHP文件上传可以实现对上传文件的各种验证,例如文件型、大小、保存路径等。通过使用PHP文件上传,可以让文件上传变得更加安全、简单和高效。 PHP文件上传可以通过封装上传文件的函数来实现文件的上传和下载。上传文件的功能通过对文件的验证和处理,将文件保存到服务器指定的位置。而下载文件的功能通过设置文件的下载头信息,实现对指定文件的下载操作。PHP文件上传还可以处理文件重名、文件大小限制、文件型限制等问题,保证文件上传过程中的安全性和完整性。 在使用PHP文件上传的过程中,需要注意对上传文件的安全性进行严格的检查,避免出现恶意文件上传或者文件被非法下载的情况。同时,需要确保服务器环境对文件上传和下载的操作进行了正确的配置,以确保文件上传和下载功能的正常运行。 总的来说,PHP文件上传可以极大地方便我们在网站开发中对文件的上传和下载操作,帮助我们完成文件处理的各种功能。无论是图片、文档还是音视频文件,PHP文件上传都可以帮助我们完成文件上传和下载功能,为网站的用户提供更好的使用体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值