zoukankan      html  css  js  c++  java
  • php上传文件代码解析

    思想;把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 "文件上传失败";
    }
    ?>
  • 相关阅读:
    redis05
    redis04
    redis03
    redis02
    C++编程学习(九)this指针&友元函数
    C++编程学习(八)new&delete动态内存分配
    ROS大型工程学习(二) 怎么阅读大型工程
    计算机操作系统学习(一) Linux常用指令(随时更新)
    ROS大型工程学习(一) 必须了解的基本文件
    单片机学习(一) 中断、定时器、优先级的一些理解
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/9958440.html
Copyright © 2011-2022 走看看