随笔 - 34  文章 - 0  评论 - 3  阅读 - 17487

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>

posted on   yanran  阅读(162)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示