思想;把html的input标签组织成一个数组,然后去重

关键技术涉及的函数 is_dir mkdir move_uploaded_file()

涉及的数组 预定义数组$_FILES

步骤一:检查上传文件夹是否存在,如果不存在就创建(注意!尽量使用绝对路径,否则可能上传失败)

$targetdir = "H:\\myphpproject\\myPHP\\upresource";
if (!is_dir($targetdir)){
mkdir($targetdir);
}

step2: 把html的input标签组织为数组,关键在于name属性要起做 xxxx[]的形式

 <form method="post" action="upfile.php" enctype="multipart/form-data">
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<br/>
<button type="submit" class="btn btn-success">提交</button>
</form>

step3:对上传文件去重,并组织上传文件的数组; 关键技术 array_unique

$arrayfile = array_unique($_FILES['myfile']['name']);

step4:循环遍历input标签获得文件名,并和目标文件夹路径组成上传文件的绝对路径

 $path = $targetdir."\\".$v;

step5:循环遍历每个input标签,上传文件 关键技术:foreach ($arrayfile as $k=>$v);上传用到 move_uploaded_file($_FILES[标签数组名][‘tmp_name’][数组索引],$v),其中tmp_name是固定用法,不必深究;$v是上传成功后,文件的绝对路径名

$res是判断上传结果的布尔型变量

foreach($arrayfile as $k=>$v)
{
$path = $targetdir."\\".$v;
if ($v)
{
if(move_uploaded_file($_FILES['myfile']['tmp_name'][$k],$path))
{
$res = TRUE;
}
else {
$res=FALSE;
}
}
}

整体既视感如下

html上传文件部分  ----input标签 type要设置成 file, enctype="multipart/form-data"不能省略

  <form method="post" action="upfile.php" enctype="multipart/form-data">
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<br/>
<button type="submit" class="btn btn-success">提交</button>
</form>

整个上传文件控制编码部分

<?php
$targetdir = "H:\\myphpproject\\myPHP\\upresource";
if (!is_dir($targetdir)){
mkdir($targetdir);
}
$arrayfile = array_unique($_FILES['myfile']['name']);
$res = FALSE;
foreach($arrayfile as $k=>$v)
{
$path = $targetdir."\\".$v;
if ($v)
{
if(move_uploaded_file($_FILES['myfile']['tmp_name'][$k],$path))
{
$res = TRUE;
}
else {
$res=FALSE;
}
}
}
if ($res==TRUE)
{
echo "文件上传成功";
}
else {
echo "文件上传失败";
}
?>

php上传文件代码解析的更多相关文章

  1. iOS上传文件代码,自定义组装body

    以下代码为上传文件所用代码,简单方便,搞了好久,终于知道这么简单的方式来上传. 其它类库也就是把这几句代码封装的乱七八糟得,让你老久搞不懂原理.不就是在body上面加点字符串,body下面加点字符串, ...

  2. ExtJS + fileuploadfield上传文件代码

    后台服务端接收文件的代码: /** * 后台上传文件处理Action */ @RequestMapping(value = "/uploadFile", method=Reques ...

  3. php 上传文件代码

    通过 PHP,能够把文件上传到server.里面加入一些图片的推断,假设不加推断文件的类型就能够上传随意格式的文件. 为了站点的安全,肯定不让上传php文件,假设有人进入你的后台,上传了一个php文件 ...

  4. java上传文件代码

    import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;impo ...

  5. PHP上传文件代码练习2 (重复文章)

    表单: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...

  6. SpringMvc通过controller上传文件代码示例

    上传文件这个功能用的比较多,不难,但是每次写都很别扭.记录在此,以备以后copy用. package com.**.**.**.web.api; import io.swagger.annotatio ...

  7. git 和码云的上传文件代码操作

    Git与Github的连接与使用 一 安装git软件 1.git介绍 ''' git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.​ 分布式相比于集中式的最大区别在于开发 ...

  8. javaWeb上传文件代码

    javaweb两种方式的上传,1普通上传,2:jquery ajax后台上传,部分截图如下: 完成包下载,下载后倒入myeclipse工程即可,下载地址:http://files.cnblogs.co ...

  9. easyui 上传文件代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;usi ...

随机推荐

  1. python爬虫概述

    爬虫的使用:爬虫用来对网络的数据信息进行爬取,通过URL的形式,将数据保存在数据库中并以文档形式或者报表形式进行展示. 爬虫可分为通用式爬虫或特定式爬虫,像我们经常用到的搜索引擎就属于通用式爬虫,如果 ...

  2. 【Linux开发】编写属于你的第一个Linux内核模块

    曾经多少次想要在内核游荡?曾经多少次茫然不知方向?你不要再对着它迷惘,让我们指引你走向前方-- 内核编程常常看起来像是黑魔法,而在亚瑟 C 克拉克的眼中,它八成就是了.Linux内核和它的用户空间是大 ...

  3. Linux 概念与快捷方式

    概念 何为shell Shell 是指"提供给使用者使用界面"的软件(命令解析器),类似于 DOS 下的 command(命令行)和后来的 cmd.exe .普通意义上的 Shel ...

  4. 如何获取字符串中某个具体的数值--通过json.load转化成字典形式获取

    r=requests.get('http://httpbin.org/get').text print(r) # print(type(r)) # 如果要获取User-Agent的详细数值,需要做JS ...

  5. Python 过滤HTML实体符号简易方法

    html_tag = {' ': '\n', '"': '\"', '&': '', '<': '<', '>': '>', '&apos; ...

  6. 记java的那些编辑器的故事之凌嘉文+李晓彤-结对编程

    [写在前面]这次是复用个人项目进行结对编程,其实主要复用的就是凌老板的出题部分和我的文件读写部分,其余部分都是新学的.在这次编程中也涨了很多知识,其中最最最让人哭笑不得的就是:两个人用了不一样的编辑器 ...

  7. [Bzoj1001][BeiJing2006]狼抓兔子(网络流/对偶图)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 看到大佬们都是对偶图过的,写了个最大流水过去了QAQ,网络流的无向图直接建双向边( ...

  8. FFmpeg4.0笔记:采集桌面

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集桌面 void test_desktop() { boo ...

  9. 一千行MySQL学习笔记 (转)

    出处:  一千行MySQL学习笔记 /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权 ...

  10. StandardWrapper

    Tomcat中有四种类型的Servlet容器,分别是 Engine.Host.Context.Wrapper,每个Wrapper实例表示一个具体的Servlet定义,StandardWrapper就是 ...