当前位置:网站首页>PHP encapsulates a file upload class (supports single file and multiple file uploads)
PHP encapsulates a file upload class (supports single file and multiple file uploads)
2022-06-24 09:56:00 【BigChen_ up】
class Upload {
// Add an intelligent action : It can automatically determine whether the current file is a multi file or a single file , To automatically select the correct method for processing
public static function save($dir) {
if (!file_exists($dir)) {
// a/b/c The intermediate directory does not exist , Automatically created !
mkdir($dir, 0777, true);
}
// Determine whether the current file is multiple still Single file upload
// An array of single file uploads yes Two dimensional array , The multi file upload array is a three-dimensional array
// practice : Judge whether it is a three-dimensional array
//reset() Used to read the first value in the array
$tmp = reset($_FILES);
$tmp = reset($tmp);
if (is_array($tmp)) {
// yes 3 dimension
return self::saveMultiple($dir);
} else {
return self::saveSingle($dir);
}
}
// Multiple file upload
protected static function saveMultiple($dir) {
//1. Transformation format
foreach ($_FILES as $key => $value) {
foreach ($value as $kk => $vv) {
foreach ($vv as $k => $v) {
$newArr[$k][$kk] = $v;
}
}
}
//2. Cycle save
$names = [];
foreach ($newArr as $key => $value) {
$filename = $value['tmp_name'];
// Random name : The nuptial and The code problem
$uniqueName = md5(microtime(true) . mt_rand(1e9, 9e9));
// Suffix name :
$ext = pathinfo($value['name'], PATHINFO_EXTENSION);
// Splice names : ***.jpg
$fn = "$uniqueName.$ext";
$dest = "files/$fn";
$suc = move_uploaded_file($filename, $dest);
// Saved successfully : Save the file name to the array
if ($suc === true) {
// Index array new elements
$names[] = $fn;
}
}
return $names;
}
// Single file upload operation
protected static function saveSingle($dir) {
foreach ($_FILES as $key => $value) {
$filename = $value['tmp_name'];
// Random name : The nuptial and The code problem
$uniqueName = md5(microtime(true) . mt_rand(1e9, 9e9));
// Suffix name :
$ext = pathinfo($value['name'], PATHINFO_EXTENSION);
// Splice names : ***.jpg
$fn = "$uniqueName.$ext";
$dest = "$dir/$fn";
$suc = move_uploaded_file($filename, $dest);
// Failure , Then return to false
if ($suc === false) {
return false;
}
}
// If there is no failure , The saved file name is returned
return $fn;
}
}
边栏推荐
- 新手怎么选择投资理财产品的等级?
- 记录一下MySql update会锁定哪些范围的数据
- How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
- impdp导schema报ORA-31625异常处理
- About thinkphp5, use the model save() to update the data prompt method not exist:think\db\query- & gt; Error reporting solution
- 如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
- 使用Live Chat促進業務銷售的驚人技巧
- 算法--找到和最大的长度为 K 的子序列(Kotlin)
- LeetCode: 240. Search 2D matrix II
- JS proxy mode
猜你喜欢

Talking about the knowledge of digital transformation

GeoGebra 实例 时钟

微信小程序學習之 實現列錶渲染和條件渲染.

R 椭圆随机点产生并画图

操作符详解

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

Amazing tips for using live chat to drive business sales

Prct-1400: failed to execute getcrshome resolution

js单例模式

5分钟,客服聊天处理技巧,炉火纯青
随机推荐
Array seamless scrolling demo
GeoGebra 实例 时钟
415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)
记录一下MySql update会锁定哪些范围的数据
达梦数据库如何定位锁等待问题解决方法
LeetCode: 240. Search 2D matrix II
PTA monkey chooses King (Joseph Ring problem)
Algorithm - the K row with the weakest combat power in the matrix (kotlin)
Producer / consumer model
Symbol.iterator 迭代器
Operator details
An open source monitoring data collector that can monitor everything
ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
如何规范化数据中心基础设施管理流程
IDEA 无法保存设置 源根 D:XXXX在模块XXX中重复
Oracle viewing data file header SCN information
latex公式及表格识别
Getting user information for applet learning (getuserprofile and getUserInfo)
Handling method of Oracle data file header SCN inconsistency
使用Live Chat促进业务销售的惊人技巧