惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

阁主学习小站

win11更新报错:net start wuauserv 发生系统错误 1290-电脑技术-阁主学习小站 Docker 常用命令大全,方便日常查阅-服务端兮-阁主学习小站 Linux系统磁盘挂载管理工具-服务端兮-阁主学习小站 Debian13 Linux系统无法关闭swap、无法完全禁用的问题和解决方案-服务端兮-阁主学习小站 minio服务器上文件访问不了-编程教程-阁主学习小站 MINIO 最新版无法通过页面的控制台配置accesskey-编程教程-阁主学习小站 centos9无法启用ssh服务-服务端兮-阁主学习小站 关闭浏览器访问http时自动转https-编程教程-阁主学习小站 前端图片<img>、链接<a>等去除referer标记,绕过防盗链-前端开发-阁主学习小站
原生PHP实现单图、多图文件批量上传-PHP教程-阁主学习小站
阁主 · 2023-08-27 · via 阁主学习小站
摘要:

本文为PHP实现单图、多图文件上传,其余文件类型原理差不多,可自行修改代码。

介绍

简单的记录下如何处理解决使用PHP处理前端上传的多图处理功能,本文只贴了图片处理的方法,其余文件类型也是差不多的。各位道友可自行修改处理代码,都是八九不离十。文末也放了代码,可自行下载学习。

代码部分

html代码部分,input的name属性my_file后面加了一个中括号[],就是以数组形式上传,后端PHP接收到的数据就更好的以数组形式接收处理。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片上传</title>
</head>

<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <p> <input type="file" name="my_file[]" multiple></p>

        <button>提交</button>
        </p>
    </form>
</body>

</html>

PHP后端代码部分,可以根据自己的项目需求适当修改,这边只作保存image类型的图片文件描述。

<?php
printf('<pre>%s</pre>', print_r($_FILES, true));

$res = upload($_FILES);
printf('<pre>%s</pre>', print_r($res, true));

// 单
// print_r(uploadFile($_FILES));
// 多
print_r(uploadFile($res));

function uploadFile(array $files, $uploadPath = 'uploads/storage'): array
{
    if (!file_exists($uploadPath)) {
        mkdir($uploadPath, 0777, true);
    }

    foreach ($files as $file) {

        if ($file['error'] == 0) {
            // echo strstr($file['type'], '/', true);

            if (strstr($file['type'], '/', true) !== 'image') {
                $tips = $file['name'] . '文件类型错误';
                continue;
            } else {

                // 确保文件名的唯一性
                $targetName = $uploadPath . '/' . date('YmdHis') . md5($file['name'] . time()) . strstr($file['name'], '.');
                // echo $targetName;
                // 将文件从临时位置 移动到指定位置
                if (!move_uploaded_file($file['tmp_name'], $targetName)) {
                    $tips = $file['name'] . '文件移动失败';
                    continue;
                } else {
                    $img[] = $targetName;
                }
            }
        }
    }

    if (!empty($tips)) {
        $res['error'] = $tips;
    } else {
        $res['fileRealPath'] = $img;
    }
    return $res;
}


// 处理多文件的格式
function upload(): array
{
    $i = 0;
    foreach ($_FILES as $k => $file) {
        // printf('<pre>%s</pre>', print_r($file, true));

        foreach ($file['name'] as $k => $v) {
            $files[$i]['name'] = $file['name'][$k];
            $files[$i]['type'] = $file['type'][$k];
            $files[$i]['tmp_name'] = $file['tmp_name'][$k];
            $files[$i]['error'] = $file['error'][$k];
            $files[$i]['size'] = $file['size'][$k];
            $i++;
        }
    }
    // printf('<pre>%s</pre>', print_r($files, true));
    return $files;
}

预览效果

效果如下图,结果为临时文件命令移动到存储目录。

临时文件命令移动到存储目录

代码附件