<?php
/*
* 功能:文件上传类
* 参数:$path:文件路径,$sql:SQL操作语句,$fileName:文件表单名称,$uploadType:文件类型指定一个数组,$fileSize文件大小
* 上传方法返回值: 0 代表上传失败,1代表上传成功,2代表文件类型和大小又问题,调用方法时候可以自行判断
* 用法:$fileup = new FileUpload("路径","文件表单名称","上传类型",上传大小);
* 日期: 2008-12-01 修改第三次
*
*/
class FileUpload
{
private $mdocpath;
private $mfilePostName;
private $muploadType;
private $mfileSize;
private $uploadfile;
private $fileName;
private $tfileName;
private $fileTypeName;
function FileUpload($path,$fileName = "0",$uploadType = "0",$fileSize = 0) //初始化上传参数
{
$this -> mdocpath = $path; // 文档上传地址
$this -> mfilePostName = $fileName; //文件表单名
$this -> muploadType = $uploadType; //文件上传类型
$this -> mfileSize = $fileSize; //文件大小
}
function fUpload() //开始上传文件 返回0表示上传失败, 返回1表示文件上传传成功,返回2表示文件类型或者超出指定大小
{
$this -> fileName = $_FILES[$this -> mfilePostName]['name'];
$this -> uploadfile = $this -> mdocpath.$_FILES[$this -> mfilePostName]['name'];
$fsize = $_FILES[$this -> mfilePostName]['size'];
if($fsize < $this -> mfileSize && $this -> checkFileType($this -> muploadType) && $this -> fileName) //判断文件大小和传入的文件类型
{
if(move_uploaded_file($_FILES[$this -> mfilePostName]['tmp_name'],$this -> uploadfile))
{
$this -> renName(); //修改文件名
return 1;
}
else
{
return 0;
}
}
else
{
//echo "出现的问题是:没选择文件,文件超出指定范围,文件类型指定类型不符合要求!";
echo $this ->getFileType();
return 2;
}
}
function fDelet($fileNames)
{
$allPath = $this -> mdocpath.$fileNames;
$is_del = @unlink($allPath);
return $is_del;
}
function checkFileType($fileType) //检测文件类型
{
$fileTypeArray = explode(",",$fileType); //分割传入的文件类型字符串
$this -> fileTypeName = $_FILES[$this -> mfilePostName]['type']; //获取当前的文件类型
if(in_array($this -> fileTypeName,$fileTypeArray)) //当前的文件类型如果存在在给定的文件类型里面则返回 真 否则返回 假
{
return true;
}
else
{
return false;
}
}
function renName() //重命文件名为时间名称
{
$fileType = explode(".",$this -> fileName);
$dates = date("imgYmdhis");
$this -> tfileName= $dates.".".$fileType[1];
$dateFilename = $this -> mdocpath.$dates.".".$fileType[1];
if(!rename($this -> uploadfile,$dateFilename))
{
echo "操作错误,请重新上传文件";
}
}
function getFilePath() //在文件上传之后调用,返回一个文件名称
{
return $this -> tfileName;
}
function getFileType()
{ //在文件上传之后调用,返回一个当前的文件类型
return $this -> fileTypeName;
}
}
?>