码迷,mamicode.com
首页 > Web开发 > 详细

PHP图片上传类

时间:2016-10-18 01:54:06      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

前言
  在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也还有必要。
一、控制器调用
public function upload_file() {
if (IS_POST) {
if (!empty($_FILES[‘Filedata‘])) {
import(‘Org.Upload‘, COMMON_PATH);
$upload = new \Upload();
// 允许上传文件大小
$upload->allowMaxSize(C(‘ALLOW_UPLOAD_FILE_MAX_SIZE‘));
// 允许上传的文件类型
$upload->allowExt(C(‘ALLOW_UPLOAD_FILE_TYPE‘));
// 获取上传文件的信息
$upload->get_upload_file_info($_FILES[‘Filedata‘]);
// 指定上传目录
$upload->root_dir(ROOT_PATH);
// 生成文件名
$file_name = $upload->upload_file_name();
// 保存到指定的目录
$res = $upload->save_file(‘Uploads/‘, $file_name);
if ($res === false) {
dump($upload->get_error());
} else {
echo ‘上传成功‘;
}
}
}
}

二、文件上传类代码

<?php

/**
* @desc 文件上传类
* @author Timothy
* Created by PhpStorm.
* Date: 2016/10/17
* Time: 12:18
*/
class Upload
{
protected $_error = ‘‘;
protected $_allowExt = array();
protected $_allowSize = 0;
protected $file_info = null;
protected $_root_dir = null;

/**
* @desc 上传文件信息
* @param array $file_info
* @return bool
*/
public function get_upload_file_info(array $file_info = array()) {
if (!is_uploaded_file($file_info[‘tmp_name‘])) {
$this->_error = ‘文件不是通过HTTP POST方式上传的‘;
return false;
}
$this->file_info = $this->_format_upload_file_info($file_info);
}

/**
* @desc 格式化上传文件的信息
* @param array $file_info
* @return array
*/
private function _format_upload_file_info(array $file_info = array()) {
$pathinfo = pathinfo($file_info[‘name‘], PATHINFO_EXTENSION);
$file_info[‘extension‘] = $pathinfo;
return $file_info;
}

// 上传文件的错误信息处理
private function _checkError($file_error = ‘‘) {
switch ($file_error) {
case UPLOAD_ERR_INI_SIZE:
$this->_error = ‘上传文件超过了PHP配置文件中upload_max_filesize选择项的值‘;
break;
case UPLOAD_ERR_FORM_SIZE:
$this->_error = ‘超过了表单MAX_FILE_SIZE限制的大小‘;
break;
case UPLOAD_ERR_PARTIAL:
$this->_error = ‘文件部分被上传‘;
break;
case UPLOAD_ERR_NO_FILE:
$this->_error = ‘没有文件被上传‘;
break;
case UPLOAD_ERR_NO_TMP_DIR :
$this->_error = ‘找不到临时文件‘;
break;
case UPLOAD_ERR_CANT_WRITE:
$this->_error = ‘文件写入失败‘;
break;
case UPLOAD_ERR_EXTENSION:
$this->_error = ‘文件类型不正确‘;
break;
default:
$this->_error = ‘系统错误‘;
break;
}
}

/**
* @desc 判断上传文件的类型
* @param string $file_type
* @return bool
*/
private function _checkExt($file_type = ‘‘) {
if (!in_array($file_type, $this->_allowExt)) {
$this->_error = ‘上传的文件类型不正确‘;
return false;
}
return true;
}

/**
* @desc 判断上传文件的大小
* @param int $file_size
* @return bool
*/
private function _checkSize($file_size = 0) {
if ($file_size > $this->_allowSize) {
$this->_error = ‘上传的图片过大‘;
return false;
}
return true;
}

/**
* @desc 处理允许上传的文件类型
* @param int $max_size
* @return bool|int
*/
public function allowMaxSize($max_size = 2) {
if (!is_numeric($max_size)) {
$this->_error = ‘允许上传的文件大小不正确‘;
return false;
}
$this->_allowSize = $max_size * 1024 * 1024;
}

/**
* @desc 处理允许上传的文件类型
* @param string $ext
*/
public function allowExt($ext = ‘‘) {
$this->_allowExt = explode(‘|‘, $ext);
}

/**
* @desc 指定路径
* @param none
* @return void
*/
public function root_dir($dir) {
$this->_root_dir = $dir;
}

/**
* @desc 递归创建目录
* @param $path
* @return bool
*/
private function _mkdir($path) {
if (!is_dir($path)) {
if (@mkdir($path, 0777, true))
return true;
else
$this->_error = ‘目录创建失败‘;
return false;
} else {
return true;
}
}

// 生成一个唯一的文件名,防止因重名而被覆盖
public function upload_file_name() {
return md5(uniqid(microtime(true), true)) . ‘.‘ . $this->file_info[‘extension‘];
}

/**
* @desc 把上传的临时文件保存到指定目录
* @param string $path
* @param string $file_name
* @return string
*/
public function save_file($path = ‘‘, $file_name = ‘‘) {
// 判断错误号
if ($this->file_info[‘error‘] > 0) {
if (!$this->_checkError($this->file_info[‘error‘])) {
return false;
}
}

// 判断是否是合法的文件类型
if (!$this->_checkExt($this->file_info[‘extension‘])) {
return false;
}

// 判断是否是合法的文件大小
if (!$this->_checkSize($this->file_info[‘size‘])) {
return false;
}

if (!$this->_checkTrueImage($this->file_info[‘tmp_name‘])) {
return false;
}

$abs_path = $this->_root_dir ? $this->_root_dir . $path : $path ;
if ($this->_mkdir($abs_path)) {
if (move_uploaded_file($this->file_info[‘tmp_name‘], $abs_path . $file_name)) {
@chmod($abs_path, 0666);
return $abs_path;
}
} else {
$this->_error = ‘上传文件失败‘;
}
}

/**
* @desc 判断是否是真实的图片
* @param string $file_info
* @return bool
*/
private function _checkTrueImage($file_info = ‘‘) {
if (!getimagesize($file_info)) {
$this->_error = ‘文件不是真实的图片‘;
return false;
}
return true;
}

/**
* @desc 获取上传错误信息,然后返回
* @return string
*/
public function get_error() {
return $this->_error;
}
}

PHP图片上传类

标签:

原文地址:http://www.cnblogs.com/timothy-lai/p/5971620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!