php上传多个图片文件

前端文件upload1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>php 上传文件</title>
</head>
<body>
    <form name="form1" method="post" enctype="multipart/form-data" action="upload1.php">

        产品图片1:<input type="file" name="uploadFile[]">    <br/>
        产品图片2:<input type="file" name="uploadFile[]">    <br/>
        产品图片3:<input type="file" name="uploadFile[]">    <br/>
        <input type="hidden" name="token"  value="upload">
        <input type="submit" value="上传">
        
    </form>
</body>
</html>  

php文件:upload1.php

<?php
header("Content-type: text/html; charset=utf-8");
if (isset($_POST['token'])&&$_POST['token']=='upload') {
    // echo "正确提交";
    print_r($_FILES);
    // die();
    $arrs = $_FILES['uploadFile'];  //三维转二维,使用更方便
    foreach($arrs['name'] as $key=>$name)
    {   
      if ($name!="")
      {
        //判断上传是否出错
        if ($arrs['error'][$key]!=0) {
            echo "<h2>文件上传出错</h2>";
            die();
        }
        //判断文件类型是否图像
        $type = $arrs['type'][$key];
        $imgs = array('image/jpeg','image/png','image/gif','image/webp'); 
        if (!in_array($type,$imgs)) {
            echo '不是图像!';
            die();
        }
        //另存文件到upload文件夹
        $tmp_name = $arrs['tmp_name'][$key];
        $ext = pathinfo($name,PATHINFO_EXTENSION);
        $dst_name = "./upload/".uniqid().'.'.$ext  ;
        echo $tmp_name.' '.$dst_name."<br>";
        move_uploaded_file($tmp_name,$dst_name);  
      }  
  
    }
}
else{
    echo "非法提交";
}

 

posted @ 2022-08-22 00:10  paoPaoLong_liu  阅读(131)  评论(0)    收藏  举报