A-A+

好用的PHP文件上传类

2014年06月16日 PHP技术文章 评论 12 条 阅读 285 views 次

虽然这个文件上传类看起来很凌乱,但却是非常好用的,其中可以定义上传的文件类型,上传的文件大小等等,主要的是可以随时开启错误记录,当我们上传文件无法成功时,会给出错误的提示,比如没有文件被上传,文件只被部分上传,上传文件上传超过了Html表单中MAX_FILE_SIZE选项指定的值,上传文件超过了xiariboke.net配置文件php.ini中upload_max_size选项指定的值,不被允许的文件类型等等。可以让我们很清楚的知道错误在哪里。。

在php中有许多优秀的文件上传类,如果文件未被上传成功,通常都会留有一串英文的错误,看起来很难懂,如果换成中文的错误字符,我们就可以第一时间知道什么地方出错了。

<?php
class FileUploadClass{
//these file name of private vars must to lower.
private $filepath;
private $allowtype=array("gif","jpg","png","jpeg","bmp");
private $maxsize=5000000;
private $randname=true;
//these file name of private vars could be lower or upper.
private $sourceName;
private $tempName;
private $fileType;
private $fileSize;
private $newFileName;
private $errorNum=0;
private $errorMsg="";

function __construct($option=array()){
foreach($option as $key=>$value){
$key=strtolower($key);
if(in_array($key,get_class_vars(get_class($this)))){
$this->setOptions($key,$value);
}else{
continue;
}
}
}

private function getErrorStr(){
$str="上传文件<font color='blue'>{$this->sourceName}</font>时出错:";
switch($this->errorNum){
case 4: $str.="<font color='red'>没有文件被上传</font>";break;
case 3: $str.="<font color='red'>文件只被部分上传</font>";break;
case 2: $str.="<font color='red'>上传文件上传超过了Html表单中MAX_FILE_SIZE选项指定的值</font>";break;
case 1: $str.="<font color='red'>上传文件超过了xiariboke.net配置文件php.ini中upload_max_size选项指定的值</font>";break;
case -1: $str.="<font color='red'>不被允许的文件类型</font>";break;
case -2: $str.="<font color='red'>文件过大,上传文件不能超过{$this->maxsize}个字节</font>";break;
case -3: $str.="<font color='red'>上传失败</font>";break;
case -4: $str.="<font color='red'>建立存放上传文件目录失败,请重新指定上传目录</font>";break;
case -5: $str.="<font color='red'>必须指定上传文件的路径</font>";break;
default: $str.="<font color='red'>未知的错误</font>";
}
return $str;
}

private function checkFilePath(){
if(empty($this->filepath)){
$this->setOptions("errorNum",-5);
return false;
}
if(file_exists($this->filepath) && is_writeable($this->filepath)){
return true;
}else{
if(@mkdir($this->filepath,0755)){
return true;
}else{
$this->setOptions("errorNum",-4);
return false;
}
}
}

private function checkFileType(){
if(in_array(strtolower($this->fileType),$this->allowtype)){
return true;
}else{
$this->setOptions("errorNum",-1);
return false;
}
}

private function checkFileSize(){
if($this->fileSize>$this->maxsize){
$this->setOptions("errorNum",-2);
return false;
}else{
return true;
}
}

private function fileRandName(){
$filename=date("YmdHis").strval(rand(10000,99999));
return $filename.'.'.$this->fileType;
}

private function setNewFileName(){
if($this->randname){
$this->setOptions("newFileName",$this->fileRandName());
}else{
$this->setOptions("newFileName",$this->sourceName);
}
}

private function setOptions($key,$value){
$this->$key=$value;
}

private function setFiles($name="",$tmp_name="",$size=0,$error=0){
$this->setOptions("errorNum",$error);

if($error){ //When $error<>0 that was true.
return false;
}

$this->setOptions("sourceName",$name);
$this->setOptions("tempName",$tmp_name);
$this->setOptions("fileSize",$size);
$arrStr=explode(".",$name);
$this->setOptions("fileType",strtolower($arrStr[count($arrStr)-1]));

return true;
}

private function copyFile(){
if(!$this->errorNum){ // This situation means $errorNum<>0.
$filepath=rtrim($this->filepath,"/")."/";
$filepath.=$this->newFileName;

if(@move_uploaded_file($this->tempName,$filepath)){
//This has a function to rename a new file name
return true;
}else{
$this->setOptions("errorNum",-3);
}
}else{
return false;
}
}

public function uploadFile($fileField){
if($this->checkFilePath()){ //We should do checkFilePath() at first sub.
}else{
$this->errorMsg=$this->getErrorStr();
return false;
}

$name=$_FILES[$fileField]["name"];
$tmp_name=$_FILES[$fileField]["tmp_name"];
$size=$_FILES[$fileField]["size"];
$error=$_FILES[$fileField]["error"];

$return=true;

if($this->setFiles($name,$tmp_name,$size,$error)){
if($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName();
if($this->copyFile()){
return true;
}else{
$return=false;
}
}else{
$return=false;
}
}else{
$return=false;
}

if(!$return){
$this->errorMsg=$this->getErrorStr();
}

return true;
}
public function getNewFileName(){
return $this->newFileName;
}
public function getErrorMsg(){
return $this->errorMsg;
}

}

?>

标签:

12 条留言  访客:12 条  博主:0 条

  1. 阿飞博客

    PHP类,不错,收藏了!

  2. 糯米汇

    话说我怕看到这么多代码 顿时就翻不动了

  3. PHP二次开发

    好多。。。

  4. 宿迁波仔博客

    博主的php确实蛮厉害啊。

  5. 屠龙

    天天看这些也会成长一点点

  6. 动漫那点事

    这个工具好像不错

  7. 逸睡博客

    很好的东西,收藏了,谢谢博主

  8. 逸睡博客

    非常的实用,谢谢博主的分享

  9. 57博客

    不错不错 支持下

  10. 恋羽

    学习了额

  11. 过来学习

  12. 圆月博客

    博客这周备案,可能要关闭博客几天了。

给我留言