文件上传是项目开发中比较常见的功能,但文件上传的过程比较繁琐,只要是有文件上传的地方就需要编写这些复杂的代码。为了能在每次开发中降低功能的编写难度,也为了能节省开发时间,通常我们都会将这些反复使用的一段代码封装到一个类中。
<?php
/**
+-----------------------------------------------------------------------------
* 文件上传类
+-----------------------------------------------------------------------------
* @author administrator
+-----------------------------------------------------------------------------
*/
class fileupload{
private $filepath; //保存路径
private $allowtype=array('gif','jpg','jpeg','png','txt');
private $maxsize=1000000; //最大允许上传大小
private $israndname=true; //是否随机
private $orginame; //原始文件名
private $tmpname; //临时文件名
private $newname; //新文件名
private $filetype; //文件类型
private $filesize; //文件大小
private $errornum=''; //错误号
private $errormsg; //错误信息
/**
+------------------------------------------------------------------------------
*构造函数
+------------------------------------------------------------------------------
* @param string $savepath 保存路径
* @param string $allowtype 允许类型
* @param string $maxsize 允许大小
*
+------------------------------------------------------------------------------
*/
function __construct($option=array()){
foreach ($option as $key=>$value){
if (!in_array($key,get_class_vars(get_class($this)))){
continue;
}
$this->setoption($key, $value);
}
}
function uploadfile($field) {
$return=true;
if (!$this->checkpath()) {
$this->errormsg=$this->geterrornum();
return false;
}
$name=$_files[$field]['name'];
$tmpname=$_files[$field]['tmp_name'];
$filesize=$_files[$field]['size'];
$error=$_files[$field]['error'];
if (is_array($name)) {
$errors=array();
for ($i=0;$i<count($name);$i++){
if ($this->getfile($name[$i],$tmpname[$i],$filesize[$i],$errors[$i])) {
if (!$this->checksize() && !$this->checktype()) {
$errors=$this->geterrornum();
return false;
}
}else{
$errors=$this->geterrornum();
return false;
}
if (!$return) {
$this->getfile();
}
}
if ($return) {
$filenames=array();
for ($i=0;$i<count($name);$i++){
if ($this->getfile($name[$i], $tmpname[$i], $filesize[$i], $filesize[$i])) {
$this->setfilename();
if (!$this->movefile()) {
$errors[]=$this->geterrornum();
$return=false;
}else{
$filenames[]=$this->getnewname();
}
}
}
$this->newname=$filenames;
}
$this->errormsg=$errors;
return $return;
}else{
if($this->getfile($name,$tmpname,filesize,$error)){
if(!$this->checksize()){
return false;
}
if(!$this->checktype()){
return false;
}
$this->setfilename();
if ($this->movefile()) {
return true;
}
}else{
return false;
}
if (!$return) {
$this->setoption('errornum', 0);
$this->errormsg=$this->geterrornum();
}
return $return;
}
}
/**
+------------------------------------------------------------------------
*设置类属性值函数
+------------------------------------------------------------------------
* @param mix $key
* @param mix $value
*/
private function setoption($key,$value){
$key=strtolower($key);
$this->$key=$value;
}
/**
+---------------------------------------------------------------------------
* 获取文件变量参数函数
+---------------------------------------------------------------------------
* @param string $name
* @param string $tmp_name
* @param number $size
* @param number $error
*/
private function getfile($name,$tmpname,$filetype,$filesize,$error=0){
$this->setoption('tmpname', $tmpname);
$this->setoption('orginame', $name);
$arrstr=explode('.', $name);
$this->setoption('filetype', $arrstr[count($arrstr)-1]);
$this->setoption('filesize', $filesize);
return true;
}
/**
+-------------------------------------------------------------------------
* 检查上传路径函数
+-------------------------------------------------------------------------
* @return boolean
*/
private function checkpath(){
if(empty($this->filepath)){
$this->setoption('errornum', -5);
return false;
}
if (!file_exists($this->filepath)||!is_writable($this->filepath)) {
if (!@mkdir($this->filepath,0755)) {
$this->setoption('errornum',-4);
return false;
}
}
return true;
}
private function is_http_post(){
if (!is_uploaded_file($this->tmpname)) {
$this->setoption('errornum',-6);
return false;
}else{
return true;
}
}
/**
+--------------------------------------------------------------------
*检查文件尺寸函数
+--------------------------------------------------------------------
* @return boolean
*/
private function checksize(){
if ($this->filesize>$this->maxsize) {
$this->setoption('errornum', -2);
return false;
}else{
return true;
}
}
/**
+---------------------------------------------------------------
* 检查文件类型函数
+---------------------------------------------------------------
* @return boolean
*/
private function checktype(){
if (in_array($this->filetype, $this->allowtype)) {
return true;
}else{
$this->setoption('errornum', -1);
return false;
}
}
private function setfilename(){
if ($this->israndname) {
$this->setoption('newname', $this->randname());
}else{
$this->setoption('newname',$this->orginame);
}
}
/**
+-----------------------------------------------------------------
* 获取新文件名
+------------------------------------------------------------------
*/
public function getnewname() {
return $this->newname;
}
private function randname(){
$rule=date("ymdhis").rand(0, 999);
return $rule.'.'.$this->filetype;
}
private function movefile(){
if ($this->errornum) {
$filepath=rtrim($this->filaepath,'/').'/';
$filepath.=$this->newname;
if (@move_uploaded_file($this->tmpname,$filepath)) {
return true;
}else{
$this->errormsg=$this->setoption('errornum',-3 );
}
}else{
return false;
}
}
/**
+----------------------------------------------------------------
* 错误信息函数
+----------------------------------------------------------------
* @return string
*/
function geterrornum() {
$erstr="上传文件<font color='red'>{$this->orginame}</font>出错";
switch ($this->errornum) {
case 4:
$erstr.="没有文件被上传";
break;
case 3:
$erstr.="文件只被部分上传";
break;
case 2:
$erstr.="上传文件超过了html表单max_file_size指定的值";
break;
case 1:
$erstr.="上传文件超过了php.ini配置文件中upload_max_filesize的值";
break;
case 0:
$erstr="上传{$this->orginame}成功";
break;
case -1:
$erstr="未允许的类型";
break;
case -2:
$erstr.="文件过大,不能超过{$this->maxsize}个字节";
break;
case -3:
$erstr.="上传失败";
break;
case -4:
$erstr="创建上传目录失败,请重新指定上传目录";
break;
case -5:
$erstr="未指定上传路径";
break;
case -6:
$erstr="非法操作";
break;
default:
$erstr.="未知错误";
}
return $erstr;
}
}
?>
以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!