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

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

阁主学习小站

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

预览效果

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

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

代码附件