猿问

如何使用PHP上传多个文件

我不知道如何修改以下代码,以使其通过选择一次上传多个文件。通过以下代码,我可以一次上传一个文件。只是希望通过此代码对其进行修改,不仅我要上传文件,还要在同一页面上显示所有已上传的文件,以供下载和删除。


    <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>

   

手掌心
浏览 178回答 1
1回答

婷婷同学_

您可以通过这种方式上传多个文件输入字段必须定义为数组,即&nbsp;images[]它应该定义为&nbsp;multiple="multiple"<input name="images[]" type="file" multiple="multiple" />// Count # of uploaded files in array$total = count($_FILES['images']['name']);// Loop through each filefor( $i=0 ; $i < $total ; $i++ ) {&nbsp; //Get the temp file path&nbsp; $tmpFilePath = $_FILES['images']['tmp_name'][$i];&nbsp; //Make sure we have a file path&nbsp; if ($tmpFilePath != ""){&nbsp; //Setup our new file path&nbsp; $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i];//Upload the file into the temp dirif(move_uploaded_file($tmpFilePath, $newFilePath)) {&nbsp; //Handle other code here&nbsp;}&nbsp;}}有关更多详细信息,PHP多次上传
随时随地看视频慕课网APP
我要回答