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

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
博客园_首页
量子位
T
Tailwind CSS Blog
博客园 - Franky
G
Google Developers Blog
D
DataBreaches.Net
Vercel News
Vercel News
B
Blog
Recent Announcements
Recent Announcements
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
爱范儿
爱范儿
博客园 - 【当耐特】
The Cloudflare Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
C
Check Point Blog
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

阁主学习小站

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

预览效果

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

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

代码附件