php 多文件上传经典代码_PHP教程
require_once("include/upload.class.php");
if($_POST["button"])
{
//print_r($_FILES);
//多个上传
//$upload = new TTRUpload($_FILES,"ANY");//同下
$upload = new TTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");
//单个上传
//$upload = new TTRUpload($_FILES["file1"]);
$upload->upload();
echo $upload->getUploadFileName();
}
?>
class TTRUpload extends Error
{
const filesize=81200000;
private $uploadpath="uploadfile/";
private $savepath=null;
private $uploadfilename=null; //单个文件为文件名,批量文件为xxxx|xxxx格式,请注意
private $ext=array("jpg","gif","png");
private $error=null;
private $file=null;
private $uploadtype=null;
private $filename=null;
//构造函数,$type:ONE单个上传 ANY批量上传;
public function __construct($file,$type="ONE")
{
if($type!="ONE" && $type!="ANY")
{
echo "";
exit;
}
$this->uploadtype=$type;
$this->file=$file;
}
private function createFileName()
{
return $this->filename="TTR_".time().$this->getRandomN(4);
}
private function getUploadPath()
{
if(substr($this->uploadpath,-1,1)!="/")
{
$this->savepath=$this->uploadpath."/".date("Ym");
}else{
$this->savepath=$this->uploadpath.date("Ym");
}
$this->savepath=$this->getFolder($this->savepath);
return true;
}
private function getFileExt($tempfilename)
{
return end(explode(".",$tempfilename));
}
private function getExt()
{
if(in_array(strtolower($this->getFileExt($tempfilename)),$this->ext))
{
return true;
}else{
return false;
}
}
private function getFolder($folder)
{
if(!is_dir($folder))
{
mkdir($folder);
}
return $folder."/";
}
public function upload()
{
if($this->uploadtype=="ONE")
{
if($this->getExt($this->file["type"]))
{
parent::errorExt();
}else if($this->file["size"]>self::filesize){
parent::errorFileSize();
}else if(!$this->getUploadPath()){
parent::errorUploadPath();
}else{
$filenametemp=$this->createFileName();
$filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file["name"]);
if(move_uploaded_file($this->file["tmp_name"],$filename))
{
$this->uploadfilename=$filenametemp;
parent::okMoved();
}else{
parent::errorMoveUpload();
}
}
}else if($this->uploadtype=="ANY"){
for($i=0;$i
{
if($this->getExt($this->file[$i]["type"]))
{
parent::errorExt();
}else if($this->file[$i]["size"]>self::filesize){
parent::errorFileSize();
}else if(!$this->getUploadPath()){
parent::errorUploadPath();
}else{
$filenametemp=$this->createFileName();
$filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file[$i]["name"]);
if(move_uploaded_file($this->file[$i]["tmp_name"],$filename))
{
$str.=$filenametemp."|";
}else{
parent::errorMoveUpload();
}
}
}
$this->uploadfilename=substr($str,0,strlen($str)-1);
parent::okMoved();
}
}
public function getUploadFileName()
{
return $this->uploadfilename;
}
public function setUploadPath($path)
{
$this->uploadpath=$path;
}
private function getRandomN($n)
{
if ($n 10) return "";
$ary_num= array(0,1,2,3,4,5,6,7,8,9);
$return ="";
for ($i=0;$i
{
$randn = rand(0,9-$i);
$return .= $ary_num[$randn];
$ary_num[$randn] = $ary_num[9-$i];
}
return $return;
}
public function __destruct()
{
$this->uploadfilename=null;
$this->uploadtype=null;
$this->file=null;
$this->savepath=null;
}
}
class Error
{
public static function errorFileSize()
{
echo "超出最大上传限制";
}
public static function errorExt()
{
echo "此类文件不允许上传";
}
public static function errorUploadPath()
{
echo "上传路径不正确";
}
public static function errorMoveUpload()
{
echo "上传失败";
}
public static function okMoved()
{
echo "上传成功!";
}
public static function okArrayMoved()
{
echo "上传成功!";
}
}

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

登录 CakePHP 是一项非常简单的任务。您只需使用一项功能即可。您可以记录任何后台进程(如 cronjob)的错误、异常、用户活动、用户采取的操作。在 CakePHP 中记录数据很容易。提供了 log() 函数

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写
