<?php
/**
* <h2>单文件上传</h2>
<form action=”index2.php” method=”post” enctype=”multipart/form-data” name=”form1″ id=”form1″>
<input type=”file” name=”file” id=”file1″ />
<input type=”submit” name=”button” id=”button” value=”Submit” />
<input type=”hidden” name=”type” value=”ONE” />
</form>
<h2>多文件上传</h2>
<form action=”index2.php” method=”post” enctype=”multipart/form-data” name=”form1″ id=”form1″>
<input type=”file” name=”file1″ id=”file1″ />
<br />
<input type=”file” name=”file2″ id=”file2″ />
<br />
<input type=”file” name=”file3″ id=”file3″ />
<input type=”submit” name=”button” id=”button” value=”Submit” />
<input type=”hidden” name=”type” value=”MORE” />
</form>
<?php
require_once(“upload.class.php”);
if($_POST[“type”] == “MORE”){
$upload = new TangUpload(array($_FILES[“file1”],$_FILES[“file2”],$_FILES[“file3″]),”MORE”);
}
else{
$upload = new TangUpload($_FILES[“file”],$_POST[“type”]);
}
$upload->upload();
if($upload->getError()){
echo “错误代码:”;
echo $upload->getError();
echo “<br>错误文件:”;
echo $upload->getErrorName();
}
else{
echo “上传成功<br>”;
echo $upload->getName();
echo “<br>”;
echo $upload->getPath();
}
?>
*
*/
class TangUpload{
private $type=”ONE”; //上传类型,包括单文件和多文件两种方式(ONE|MORE)
private $size = 307200; //上传文件大小限制(300K)
private $ext = array(“jpg”,”gif”,”png”,”jpeg”,”bmp”); //上传文件类型限制
private $folder = “upload”; //保存上传文件的一级文件夹名
private $file = null; //文件资源,多文件为资源数组
private $name = null; //上传后的文件名
private $path = null; //文件保存目录
private $error = 0; //操作状态值(0成功|1空文件|2类型错误|3超过大小|4上传错误)
private $errorName = null; //操作后第一个出现错误的文件名
/**
* 设置保存文件夹
*/
public function setBaseFolder($folder)
{
$this->folder = $folder;
}
/**
* 属性 取得上传后的文件名
*/
public function getName(){
return $this->name;
}
/**
* 属性 取得文件保存目录
*/
public function getPath(){
return $this->path;
}
/**
* 属性 取得文件保存目录
*/
public function getError(){
return $this->error;
}
/**
* 属性 取得出错的文件名
*/
public function getErrorName(){
return $this->errorName;
}
/**
* 构造函数
* $_file :文件资源
* $_type :上传类型
*/
public function __construct($_file , $_type=null){
if($_type == “MORE”){
$this->type = $_type;
}
$this->file = $_file;
}
/**
* 主函数
*/
public function upload(){
if($this->type == “MORE”){
$count = count($this->file);
for($i=0;$i<$count;$i++){
$this->checkFile($this->file[$i]);
if($this->error){
$this->errorName = $this->file[$i][“name”];
bleak;
}
}
if(!$this->error){
$name = null;
$path = null;
for($i=0;$i<$count;$i++){
$this->uploadOne($this->file[$i]);
if($this->error){
$this->errorName = $this->file[$i][“name”];
bleak;
}
else{
$name .= $this->name.”,”;
$path .= $this->path.”,”;
}
}
$this->name = substr($name,0,strlen($name)-1);
$this->path = substr($path,0,strlen($path)-1);
}
}
else{
$this->checkFile($this->file);
if($this->error){
$this->errorName = $this->file[“name”];
}
else{
$this->uploadOne($this->file);
if($this->error){
$this->errorName = $this->file[“name”];
}
}
}
}
/**
* 析构函数
*/
public function __destruct(){
$this->file = null;
$this->name = null;
$this->path = null;
$this->error = 0;
$this->errorName = null;
}
/**
* 对上传文件进行检查
* $_file :文件名
*/
private function checkFile($_file){
if($_file[“size”] < 1){
$this->error = 1; //判断文件是否为空
}
else if($this->checkFileType($_file[“name”])) {
$this->error = 2; //判断文件类型是否被允许
}
else if($_file[“size”] > $this->size){
$this->error = 3; //判断文件大小是否被允许
}
}
/**
* 保存单个文件
* $_file :文件名
*/
private function uploadOne($_file){
$path = $this->getFilePath();
$name = $this->getFileName();
$this->name = $name.”.”.$this->getFileType($_file[“name”]);
$this->path = $path.$this->name;
$result = move_uploaded_file($_file[“tmp_name”],$this->path);
chmod($this->path,0777);
if(!$result){
$this->error = 4; //检查从PHP临时目录保存过来是否出错
}
}
/**
* 通过文件名检查文件类型是否被允许上传
* $_name :文件名
* return 是否被允许上传
*/
private function checkFileType($_name){
$fileType = strtolower($this->getFileType($_name));
$result = in_array($fileType,$this->ext);
if($result){
return false;
}
else{
return true;
}
}
/**
* 通过文件名后缀取得文件类型
* $_name :文件名
* return 文件名后缀
*/
private function getFileType($_name){
return end(explode(“.”,$_name));
}
/**
* 创建文件保存目录
* return 文件保存目录
*/
private function getFilePath(){
if(substr($this->folder,-1,1) != “/”){
$dir = “/”;
}
else{
$dir = “”;
}
$dir = $this->folder.$dir.date(“Ym”).”/”;
$this->mkdirs($dir);
return $dir;
}
/**
* 通过递归创建文件保存目录
* $_dir :文件目录
*/
private function mkdirs($_dir){
if(!is_dir($_dir)){
$this->mkdirs(dirname($_dir));
//echo $_dir;
mkdir($_dir,0777); //linux系统要注意文件夹的读写权限,加相应的参数,比如0777
}
}
/**
* 创建文件名
* return 文件名
*/
private function getFileName(){
return ‘jack_’.time().$this->getRandom();
}
/**
* 创建4位随机数
* return 4位随机数
*/
private function getRandom(){
$string = ”;
$ary_num= array(0,1,2,3,4,5,6,7,8,9);
for ($i=0;$i<4;$i++){
$rand = rand(0,9);
$string .= $ary_num[$rand];
}
return $string;
}
}