使用MOVE_TO_FILE

时间:2019-01-03 06:22:02

标签: php html

这是一个更清晰的问题。如果我从类似这样的表格中插入:

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

进入数据库以获取链接,并且成功:

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

只是一个例子:

但是在php脚本中:

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

由于tmp_name文件夹仅将上传文件保存在数组中,因此我不得不使用foreach或for loop搜索文件循环,这比INSERT INTO db困难。

问题是如何将搜索结果与数组分开,然后将每个结果插入数据库?

这里是代码:

     <?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 个答案:

答案 0 :(得分:0)

这是一个示例,您可以根据需要对其进行熟练处理。

<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代码就像

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

                }
            }
        }
    }
?>