写了一个文件上传类 此类可复用~

<?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;
}
}
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值