• Hi,
    how to use wp_handle_upload for multiple files.
    this is input box…

    <input class=”test” id=’upload’ name=”upload[]” type=”file” multiple=”multiple” required/>
    not getting how to use…

    Thanks
    Prakash

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello

    Please try using the below code this will help you.

    <?php
     if( 'POST' == $_SERVER['REQUEST_METHOD']  ) {
    if ( $_FILES ) {
        $files = $_FILES["my_file_upload"];
        foreach ($files['name'] as $key => $value) {
                if ($files['name'][$key]) {
                    $file = array(
                        'name' => $files['name'][$key],
                        'type' => $files['type'][$key],
                        'tmp_name' => $files['tmp_name'][$key],
                        'error' => $files['error'][$key],
                        'size' => $files['size'][$key]
                    );
                    $_FILES = array ("my_file_upload" => $file);
                    foreach ($_FILES as $file => $array) {
                        $newupload = my_handle_attachment($file,$pid);
                    }
                }
            }
        }
    
    }
    ?>
    in function.php
    
    function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
    $attach_id = media_handle_upload( $file_handler, $post_id );
    if ( is_numeric( $attach_id ) ) {
    update_post_meta( $post_id, '_my_file_upload', $attach_id );
    }
    }

    Thanks

    Thread Starter harshap

    (@harshap)

    Thanks for reply
    clarion technologies…

    Will you please tell me how to upload a simple file to upload
    here is my code..
    if(isset($_POST[‘submit’]))
    {

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

    //Make sure we have a filepath
    if($tmpFilePath != “”){

    //save the filename
    $shortname = $_FILES[‘upload’][‘name’][$i];

    //save the url and the file
    $filePath = “WP_SITEURL/wp-content/uploads/product/” .$_FILES[‘upload’][‘name’][$i];

    //Upload the file into the temp dir

    if(move_uploaded_file($tmpFilePath, $filePath)) {

    $files[] = $shortname;

    }
    }

    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_handle_upload’ is closed to new replies.