为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用PHP上传多个文件

如何使用PHP上传多个文件

PHP
手掌心 2021-04-27 17:53:21
我不知道如何修改以下代码,以使其通过选择一次上传多个文件。通过以下代码,我可以一次上传一个文件。只是希望通过此代码对其进行修改,不仅我要上传文件,还要在同一页面上显示所有已上传的文件,以供下载和删除。    <html>    <title>Brief upload</title>    <link href="globe.png" rel="shortcut icon">    <?php    date_default_timezone_set("Asia/Calcutta");    //echo date_default_timezone_get();    ?>    <?php    $conn=new PDO('mysql:host=localhost; dbname=deu', 'root', '') or die(mysql_error());    if(isset($_POST['submit'])!=""){      $name=$_FILES['photo']['name'];      $size=$_FILES['photo']['size'];      $type=$_FILES['photo']['type'];      $temp=$_FILES['photo']['tmp_name'];      $date = date('Y-m-d H:i:s');      $caption1=$_POST['caption'];      $link=$_POST['link'];      move_uploaded_file($temp,"files/".$name);    $query=$conn->query("INSERT INTO upload (name,date) VALUES ('$name','$date')");    if($query){    header("location:index.php");    }    else{    die(mysql_error());    }    }    ?>    <html>    <body>    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen">    <link rel="stylesheet" type="text/css" href="css/DT_bootstrap.css">    <link rel="stylesheet" type="text/css" href="css/font-awesome.css">    <link rel="stylesheet" href="css/bootstrap.min.css">    <link rel="stylesheet" href="font-awesome/css/font-awesome.min.css"/>    </head>    <script src="js/jquery.js" type="text/javascript"></script>    <script src="js/bootstrap.js" type="text/javascript"></script>    <script type="text/javascript" charset="utf-8" language="javascript" src="js/jquery.dataTables.js"></script>    <script type="text/javascript" charset="utf-8" language="javascript" src="js/DT_bootstrap.js"></script>    <?php include('dbcon.php'); ?>    <style>   
查看完整描述

1 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

您可以通过这种方式上传多个文件

  1. 输入字段必须定义为数组,即 images[]

  2. 它应该定义为 multiple="multiple"

<input name="images[]" type="file" multiple="multiple" />


// Count # of uploaded files in array

$total = count($_FILES['images']['name']);


// Loop through each file

for( $i=0 ; $i < $total ; $i++ ) {


  //Get the temp file path

  $tmpFilePath = $_FILES['images']['tmp_name'][$i];


  //Make sure we have a file path

  if ($tmpFilePath != ""){

  //Setup our new file path

  $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i];


//Upload the file into the temp dir

if(move_uploaded_file($tmpFilePath, $newFilePath)) {


  //Handle other code here


 }

 }

}

有关更多详细信息,PHP多次上传


查看完整回答
反对 回复 2021-05-07
  • 1 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信