使用表单中的1个字段发布多个图像

时间:2014-04-09 09:12:34

标签: php wordpress

我的代码在这里。如何使用1个输入字段上传1个以上的图像?

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

答案 0 :(得分:0)

html: -

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

PHP代码: -

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

    }
  }
}

我接受此代码: -
Multiple file upload in php
在发布任何问题之前,您应该搜索问题.... !!!