温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP 上传多个文件

发布时间:2020-08-04 00:39:54 来源:网络 阅读:301 作者:津沙港湾 栏目:web开发

index.php页面表单页面

    <form action="test.php" method="post" enctype="multipart/form-data">
    name:<input type="text" name="username" value="" /><br/>
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
    up pic:<input type="file" name="picture[]" value=""/><br/>
    up pic:<input type="file" name="picture[]" value=""/><br/>
    up pic:<input type="file" name="picture[]" value=""/><br/>
    <input type="submit" value="upload" /><br/>
    </form>

test.php页面 处理提交信息页面

<?php 
header('content-type:text/html;charset=utf-8');
//var_dump($_POST);
//var_dump($_FILES);
/*
 * 单个文件上传
 * array (size=2)
  'username' => string 'yang' (length=4)
  'MAX_FILE_SIZE' => string '1000000' (length=7)

array (size=1)
  'picture' => 
    array (size=5)
      'name' => string 'PHP.jpg' (length=7)
      'type' => string 'p_w_picpath/jpeg' (length=10)
      'tmp_name' => string 'D:\wamp\tmp\php7C.tmp' (length=21)
      'error' => int 0
      'size' => int 279468    
      
 *
 *多个文件上传
 *array (size=2)
  'username' => string 'xiaohua' (length=7)
  'MAX_FILE_SIZE' => string '1000000' (length=7)

array (size=1)
  'picture' => 
    array (size=5)
      'name' => 
        array (size=3)
          0 => string 'PHP.jpg' (length=7)
          1 => string 'PHP.jpg' (length=7)
          2 => string 'PHP.jpg' (length=7)
      'type' => 
        array (size=3)
          0 => string 'p_w_picpath/jpeg' (length=10)
          1 => string 'p_w_picpath/jpeg' (length=10)
          2 => string 'p_w_picpath/jpeg' (length=10)
      'tmp_name' => 
        array (size=3)
          0 => string 'D:\wamp\tmp\php89.tmp' (length=21)
          1 => string 'D:\wamp\tmp\php8A.tmp' (length=21)
          2 => string 'D:\wamp\tmp\php8B.tmp' (length=21)
      'error' => 
        array (size=3)
          0 => int 0
          1 => int 0
          2 => int 0
      'size' => 
        array (size=3)
          0 => int 279468
          1 => int 279468
          2 => int 279468
 
 *
 */
 
$num = count($_FILES['picture']['name']);
for($i=0;$i<$num;$i++){//设置多个文件上传
    //第一步:判断错误
    if($_FILES['picture']['error'][$i]>0){
        switch ($_FILES['picture']['error'][$i]){
            case 1 :
                echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。';
                break;
            case 2 :
                echo '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 ';
                break;
            case 3 :
                echo '文件只有部分被上传。';
                break;
            case 4 :
                echo '没有文件被上传。';
                break;
            case 6 :
                echo '找不到临时文件夹。';
                break;
            case 7 :
                echo '文件写入失败。';
                break;
            default:
                echo '未知错误';
        }
        //exit;
        continue;
    }
    
    //第二步 判断类型
    $arr = explode(".",basename($_FILES['picture']['name'][$i]));    
    $ext = array_pop($arr);    
    $allowType = array("gif","png","jpg","jpeg");
    if(!in_array($ext, $allowType)){
            echo '上传的类型不合法';
            exit;
    }
    
    //第三步 判断大小
    $maxsize = 1000000;
    if($_FILES['picture']['size'][$i]>$maxsize){
            echo "上传的文件超过最大存储值{$maxsize}字节";
            exit;
    }
    
    //第四步 上传后的文件名 一定要设置随机文件名
    
    $tmpfile = $_FILES['picture']['tmp_name'][$i]; 
    $destname = "./uploads/".date('YmdHis').rand(99,99999).".".$ext;//设置随机文件名
   //将临时目录下的上传的文件,复制到指定的新目录,就算上传成功。
   if(move_uploaded_file($tmpfile, $destname)){
       echo "上传成功!";
   }else{
       echo "上传失败!";
   }
   
}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI