zoukankan      html  css  js  c++  java
  • php 多文件上传

    注意事项:

      当命名时用到的是数组形式时,与单独命名不同

      如:pic[],

      则会表现出这种结果

    file不允许有默认值

    <?php

    function _mkdir(){
        $dir=date('md/i',time());
        if(is_dir('./'.$dir)){
            return $dir;
        }else{
            mkdir('./'.$dir,0777,true);
            return $dir;
        }
    }
    /*获取文件后缀*/
    function getExt($file){
        $tmp=explode('.',$file);
        return end($tmp);
    }
    /*生成随机文件名*/
    function randName(){
        $str='abcdefghijklmnopqrstuvwxyz0123456789';
        return substr(str_shuffle($str),0,6);  
    }

    foreach($_FILES as $k=>$v){
        $path='./'._mkdir().'/'.randName().'.'.getExt($v['name']);
        if($v['error']!=0){
            echo $k,'上传失败';
            echo '错误代码是'.$v['error'],'<br/>';
            continue;
        }
        if(move_uploaded_file($v['tmp_name'],$path)){
            echo $k,"上传成功<br/>";
        }else{
            echo "失败";
        }
    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
    <head>
    <title>新建网页</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    </style>
    </head>
        <body>
            <h2>上传案例-分目录存储</h2>
            <form action="up3.php"method="post" enctype="multipart/form-data">
                    用户名:<input type="text" name="username"/><br/>
                    头像:<input type="file" name="avatar"/><br/>
                     图片:<input type="file" name="pic"/><br/>
                     简历:<input type="file" name="resume"/><br/>
                    <input type="submit" value="提交"/>
            </form>
        </body>
    </html>

  • 相关阅读:
    BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(分块)
    BZOJ 4241 历史研究(分块)
    BZOJ 3110 [Zjoi2013]K大数查询(整体二分)
    hdu 5412 CRB and Queries(整体二分)
    POJ2104 K-th Number(整体二分)
    luogu P3157 [CQOI2011]动态逆序对(CDQ分治)
    陌上开花(CDQ分治)
    BZOJ 1176[Balkan2007]Mokia(CDQ分治)
    BZOJ 3626 LCA(离线+树链剖分+差分)
    bzoj1592 Making the Grade
  • 原文地址:https://www.cnblogs.com/yanran/p/5021172.html
Copyright © 2011-2022 走看看