drl2051 2019-01-03 06:22
浏览 46

使用MOVE_TO_FILE将文件输入插入到BD中

This is more of a question to gain a more clear understanding. If I insert from a form like:

 <form action="upload.php" method="post" enctype="multipart/form-data">
   Select image to upload:
   <input type="file" name="fileToUpload" id="fileToUpload">
   <input type="file" name="fileToUpload" id="fileToUpload">
   <input type="submit" value="Upload Image" name="submit">
  </form>

into the DB for a link and it's successful:

 $file = "INSERT INTO ('foo') VALUES ('FOO', NOW())";

just an example:

yet in the php script:

  $_FILE['fileToUpload']['name'];
  $_FILE['fileToUpload']['tmp_name'];

since the tmp_name folder only holds upload files in an array, which than I would have to use either a foreach or for loop search the loop of files, this than makes the INSERT INTO db difficult.

Question is how can I separate the search results from the array and than insert each one into the database?

HERE"S THE CODE:

     <?php
    $con = mysqli_connect("localhost","root","","acc1");
    if(mysqli_connect_errno()){
        echo 'Failed to connect to MySQL:' . mysqli_connect_error();
    }else{
        echo 'Connected!';
    }
    if(isset($_POST['submit']) && !empty($_FILES['fileBC']['name'] && !empty($_FILES['fileB']['name'] && !empty($_FILES['fileBR']['name']) ))){

     $file = "image/";
     $name = $_FILES['fileBC']['name'];
     $data = $_FILES['fileBC']['tmp_name'];
     $fileV = "video/";
     $nameV = $_FILES['fileBR']['name'];
     $dataV = $_FILES['fileBR']['tmp_name'];
     $fileB = "book/";
     $nameB = $_FILES['fileB']['name'];
     $dataB = $_FILES['fileB']['tmp_name'];
     if(move_uploaded_file($data,$file.$name)){
         $ins_name = $con->query("INSERT INTO fileimages (fileBC, fileBR, fileB) VALUES ('$name','$nameB', '$nameV')");
     }if($ins_name){ 
         echo 'success!';
    }else{
        echo 'error';
    }
  }

     ?>
     <!DOCTYPE html>
      <html> 

       <head>
     <link rel="stylesheet" href="css/bootstrap.css">
     <link rel="stylesheet" href="css/fontawesome.css">
     <script src="js/jquery.js"></script>
     <script src="js/bootstrap.js"></script>
    </head>
    <script>
     function mymodal(){
     $('#myModal').modal('show');

    }

    </script>
    <body>
    <form method="post" action="index.php" enctype="multipart/form-data">
        <div class="form-group">
            <label class="text-primary">Book Cover:</label>
            <input  class="form-control" type="file" name="fileBC" accept="image/*" >
           </div>
           <div class="form-group">
            <label class="text-primary">Book:</label>
            <input  class="form-control" type="file" name="fileB" accept=".epub, .mobi, .pdf, .prc, .azw, .bbeb" >
           </div>
           <div class="form-group">
            <label class="text-primary">Book Reading:</label>
            <input class="form-control" type="file" name="fileBR" accept="video/*" >
           </div>
        <button name="submit">upload</button>
    </form>
     <p></p>
    <ol>

     </ol>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • douzhen9428 2019-01-03 06:34
    关注

    This is an example you can adept it as per your need.

    <form action="file_reciever.php" enctype="multipart/form-data" method="post">
    <input type="file" name="files[]" multiple/>
    <input type="submit" name="submission" value="Upload"/>
    </form>
    

    PHP code is like

    <?php
        if (isset($_POST['submission'] && $_POST['submission'] != null) {
            for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
                //Get the temp file path
                $tmpFilePath = $_FILES['files']['tmp_name'][$i];
    
                //Make sure we have a filepath
                if ($tmpFilePath != "") {
                    //Setup our new file path
                    $newFilePath = "./uploadFiles/" . $_FILES['files']['name'][$i];
    
                    //Upload the file into the temp dir
                    if (move_uploaded_file($tmpFilePath, $newFilePath)) {
    
                        //Your insert statement goes here
    
                    }
                }
            }
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。