PHP文件下载上传类

时间:2022-09-29 18:53:50
刚学了一点PHP,自己觉得入门很快,其实也很简单,其思维就是从其C、C++语言中搬迁而来。有很多不足之处,大家指出来:
class fileup
{
private $filePath;//文件路径
private $fileField;//当前文件
private $originName;//文件原名
private $tmpFileName;//临时文件名
private $fileType;//文件类型
private $filesize;//文件大小
private $newFileName;//上传文件名
private $allowType=array('txt','doc','pdf','docx','jpeg','zip','xml','html','js','jpg','gif','png','doc','swf','rar','zip');
private $MaxSzie=10000000000;
private $IsUserDefName=false;
private $randName;//随机文件名
private $ErrorNum=0;//错误现实,file上床等
private $isCoverMode=true;
private $extension;//文件拓展名
private $fileCount=0;
function __construct($filepath)
{
$this->filePath=$filepath;
$this->getInfor();
file_type();
}
function uploadFile($fileup)
//这里传来的是$_FILE['name']
{

$this->newFileName=$_SERVER['server_time'].$fileup['name'];
if($fileup['size']>$this->filesize)
{
return ;
}
if(!in_array($fileup['type'],$this->allowType))
{
return ;
}
if(is_uploaded_file($fileup['tmp_name']))
{
return ;
}
if(move_uploaded_file($fileup['tmp_name'],$this->fileField.$this->newFileName))
{
return 1;
}
//表示为上传成功
}
function downloadFile()
{
header("Content-Type:".$this->fileType);
header("Content-Disposition:attachment;filename='".$this->newFileName."'");
header("Content-Length:".$this->filesize);
header($this->newFileName);
}
private function getInfor()
{
$this->fileField=pathinfo($this->filePath);
$file=basename($this->filePath);
$this->filesize=filesize($file);
$infor=pathinfo($file);
$this->extension=$infor['extension'];

}
function show_files()
{
$this->readfile($this->filePath);
echo "the acount of file is ".$this->fileCount."<br/>";
}
private function readfile($file_show)
{

$file=opendir($file_show);
while($files=readdir($file))
{
if(is_file($files))
{
echo $file.'\\'. $files."<br>";
$this->fileCount++;
}
if(is_dir($files))
{
readdir($files);
}
}
}
private function file_type()
{
switch ($this->extension)
{
case 'html':
case "log":
case "php":
case "phtml":
$this->fileType="text/plain";
break;
case "css":
$this->fileType="text/css";
break;
case "xml":
case "xsl":
$this->fileType="text/xml";
break;
case "js":
$this->fileType="text/javascript";
break;
case "gif":
$this->fileType="image/gif";
break;
case "jpeg":
case "jpg":
$this->fileType="image/jpg";
break;
case "png":
$this->fileType="image/png";
break;
case "pdf":
$this->fileType="application/pdf";
break;
case "doc":
case "dot":
case "docx":
$this->fileType="application/msword";
break;
case "zip":
$this->fileType="application/zip";
break;
case "rar":
$this->fileType="application/rar";
break;
case "swf":
$this->fileType="application/x-shockwave-flash";
break;
case "bin":
case "exe":
case "com":
case "dll":
case "class":
$this->fileType="application/octet-steam";
break;
}
}

}
//这里是单文件上传,多文件上传也是同理,无非是$_FILE['']['tmp_name'][size]上传的文件等多少等一个数组,前台有form表单等mutifile,name为一个数组就可以了post方式,ajax上传。后台解析保存--本地或有数据库文件名
自己是新手,完全自己学的,能力有限,希望大家帮助一下。