又花了半天时间写了个php上传类

aier520 2012-05-02 10:39:07
刚学PHP没多久,为了更好的练习及熟悉PHP,自己花了大半天写了个php上传类,在这里做个笔记,欢迎朋友们对这个类做修改及优化。


//up_class.php

<?php
class UpFile{
private $inputFile; //文件域名
private $tmpName; //临时文件名
private $tmpPath; //临时文件路径
private $savePath; //保存路径
private $reType; //返回类型
private $upMaxSize; //上传文件大小限制
private $allowFile; //允许上传的文件
private $upFolder; //要上传到的文件夹
private $isReName; //是否要将上传的文件重命名
private $endFileName; //最终保存的文件名
public $fileSize; //文件大小
public $fileType; //文件类型
public $errorInt; //上传失败及错误原因


/**
* @param
* $inputFile(表单内文件域名称);</br>
* $upFolder(保存到服务器的文件夹);</br>
* $isRename(对上传文件重命名,值 有 y|n );</br>
* $reType(上传成功后返回的值:n是返回文件名,pn:返回路径和文件名,j:返回js[待扩展]);</br>
* $upExt(要上传的文件分类,因为在不同的表单要限制不同的上传文件类型,比如 A表单只能上传图片,B表单只能上伟压缩包);</br>
* $maxSize(限制上传的文件大小)</br>
* @author:256kb
* @2012-5-1
*/
public function __construct($inputFile , $upExt = 0 , $reType = 'pn' , $upFolder = 'upload/' , $isRename = 'y' , $maxSize = 10485760){
$this->inputFile = $inputFile;
$this->reType = $reType;
$this->upMaxSize = $maxSize;
$this->allowFile = $upExt;
$this->upFolder = $upFolder;
$this->isReName = $isRename;
//$this->errorInt = -1;
}

public function upFile(){
$_file_arr = $_FILES[$this->inputFile];
$this->errorInt = $_file_arr['error'];
if(is_uploaded_file($_file_arr['tmp_name'])){
if($_file_arr['tmp_name']){
$this->tmpName = $_file_arr['name'];
$this->upMaxSize = $_file_arr['size'];
$this->fileType = $_file_arr['type'];
$this->tmpPath = $_file_arr['tmp_name'];
$this->fileSize = $_file_arr['size'];
if($this->upMaxSize > $this->upMaxSize){
$this->errorInt = 6 ; //大小超出网站限制
}

if(!$this->isAllow()){
$this->errorInt = 8 ; //系统不允许此类型文件
}

if($this->isReName=='y'){
$this->savePath = $this->upFolder.$this->getFolder().'/'.$this->getNewName() ;
$this->endFileName = $this->getNewName();
}else{
$this->savePath = $this->upFolder.$this->getFolder().'/'.$this->tmpName ;
$this->endFileName = $this->tmpName;
}
//echo $this->errorInt;
if(!$this->errorInt >= 1){
move_uploaded_file($this->tmpPath,$this->savePath);
}

}
}
}

public function getFileUrl(){
switch($this->reType){
case 'n':
return $this->endFileName;
break;
case 'pn':
return $this->savePath;
break;
case 'js':
return "<script language=\"javascript\" type=\"text/javascript\">window.parent.LoadAttach('".$this->savePath."');</script>";
break;
default:
return $this->savePath;
}
}

//获得新文件名
public function getNewName(){
return substr($this->tmpName,1,strrpos($this->tmpName,".")-1).'_'.mktime().'.'.$this->getFileExt();
}

public function upStatus(){
//echo $this->errorInt;
switch ($this->errorInt){
case 1:
return '超过了文件大小php.ini中限制大小';
break;
case 2:
return '超过了文件大小MAX_FILE_SIZE 选项指定的值';
break;
case 3:
return '文件只有部分被上传';
break;
case 4:
return '没有文件被上传';
break;
case 5:
return '上传文件大小为0';
break;
case 6:
return '大小超出网站限制';
break;
case 7:
return '网站内没有指定这种上传类型';
break;
case 8:
return '系统不允许此类型文件';
case 9:
return '创建目录失败!';
break;
}
}

private function isAllow(){
$allow = array(
0 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png','application/x-zip-compressed','application/octet-stream'),
1 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'),
2 => array('application/x-zip-compressed','application/octet-stream'),
3 => array('','',''),
4 => array('','','')
);
if($this->allowFile > count($allow)-1){
$this->errorInt = 7; //网站内没有指定这种上传类型
}else{
if(in_array($this->fileType, $allow[$this->allowFile])){
return true;
}else{
return false;
}
}
}

public function getFileExt(){ //获得文件扩展名
return strtolower(substr($this->tmpName,strrpos($this->tmpName,".")+1));
}

private function getFolder(){ //获得并自动创建相应文件夹
if(strpos('|rar|zip|7z|iso|','|'.$this->getFileExt().'|')>=0){
$_folder = 'rar';
}elseif(strpos('|gif|jpeg|jpg|png|bmp|pjpeg|psd|','|'.$this->getFileExt().'|')>=0){
$_folder = 'img';
}elseif(strpos('|rm|rmvb|avi|mp4|swf|flv|wmv|','|'.$this->getFileExt().'|')>=0){
$_folder = 'vide';
}elseif(strpos('|doc|txt|xls|mdb||','|'.$this->getFileExt().'|')>=0){
$_folder = 'doc';
}else{
$_folder = 'other';
}
if(!file_exists($this->upFolder.$_folder)){
if(!mkdir($this->upFolder.$_folder)){
$this->errorInt = 9; //创建目录失败
}
}
return $_folder;
}

}



实例



<?php
header("Content-type: text/html; charset=utf-8");
include './class/up_class.php';

if(isset($_POST['bottom'])){
$up = new UpFile('ufile',0,'js');
$up->upFile();
echo $up->fileType."<br />"; //文件类型
echo $up->fileSize."<br />"; //文件大小
echo $up->upStatus()."<br />"; //如果没上传成功,显示文件上传失败原因
echo $up->getFileExt()."<br />";//文件扩展名
echo $up->getFileUrl()."<br />";//文件在服务器的路径
echo $up->getNewName()."<br />";//上传后文件新名称

}
?>



<html>
<head>
<title>test upload</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="file" id="ufile" name="ufile" />
<input type="submit" name="bottom" id="bottom" value="upstart" />
</form>

</body>

</html>
...全文
326 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
青葱的岁月 2012-05-02
  • 打赏
  • 举报
回复
不错,我自学好长时间了 到现在还是迷迷糊糊。能否一起学。
xuzuning 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

多文件搞个循环取文件名就OK了
[/Quote]是吗?
如果写作
<input type="file" id="ufile" name="ufile1" />
<input type="file" id="ufile" name="ufile2" />
是可以的

如果写作
<input type="file" id="ufile" name="ufile[]" />
<input type="file" id="ufile" name="ufile[]" />
你在哪里循环?

既然是写成类,当然要考虑的多一些,使用起来方便一些
aier520 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
不支持多文件上传?
[/Quote]
之前想过了,但在类里面直接操作多文件上传不太方便,而且现实中上传文件都要对单个文件进行一些处理,如获得文件大小,文件路径,扩展名什么的,如果做成多文件上传,一些返回值使用起来就比较麻烦了。所以后来没有弄
小在在 2012-05-02
  • 打赏
  • 举报
回复
多文件搞个循环取文件名就OK了
xuzuning 2012-05-02
  • 打赏
  • 举报
回复
不支持多文件上传?
coder 2012-05-02
  • 打赏
  • 举报
回复
珍藏啊,有人再问了。就有的贴了
  • 打赏
  • 举报
回复
O(∩_∩)O谢谢分享
happyrenyu 2012-05-02
  • 打赏
  • 举报
回复
支持一下

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧