post multiple image using 1 field in a form

2019-09-01 08:43发布

问题:

My code in here. how can I upload more then 1 image using 1 input field?

<?php
if (isset($_POST['beopen_form'])) {
if ($_FILES["file1"]["error"] > 0) {
    echo "Error: " . $_FILES["file1"]["error"] . "<br>";
} else {
    $x          = mysql_insert_id();
    $path_array = wp_upload_dir();
    $old_name   = $_FILES["file1"]["name"];
    $split_name = explode('.', $old_name);
    $time       = time();
    $file_name  = $time . "." . $split_name[1];
    $tmp_name   = $_FILES["file1"]["tmp_name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], $path . "/" . $old_name);
    $path2 = $path . "/" . $old_name;
    mysql_query("INSERT INTO carimage (image,carid,created,updated) VALUES ('$path2','$x','$created','$updated') ON DUPLICATE KEY UPDATE image='$path2',description='$makeid',updated='$updated'");
}
}
?>
<form enctype="multipart/form-data" class="beopen-contact-form" id="signupForm" novalidate="novalidate" action="<?php echo get_permalink(); ?>" method="post"> 
    <input type="file" name="file" id="file">
    <div id="recaptcha_div"></div><br>
    <input type="hidden" name="beopen_form" value="1" /><!-- button  -->
    <button class="button send-message" type="submit">
        <span class="send-message"></span><?php _e('Update', 'beopen');?>
    </button>    
</form>

回答1:

html :-

<input type="file" name="file" id="file" multiple>

PHP CODE:-

//Loop through each file
for($i=0; $i<count($_FILES['file']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['file']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['file']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

I TAKE THIS CODE FROM :-
Multiple file upload in php
Before Post any question you should search for problem....!!!



标签: php wordpress