6

I'm working on a plugin for wordpress and I want to be able to upload multiple pictures from a form. Right now when I have a form for two pictures and submit it empty, my $_FILES array looks like this:

Array (
    [image] => Array ( 
        [name] => Array ( 
            [1] => 
            [2] =>
        ) 
        [type] => Array ( 
            [1] => 
            [2] =>
        ) 
        [tmp_name] => Array ( 
            [1] => 
            [2] =>
        ) 
        [error] => Array ( 
            [1] => 4 
            [2] => 4
        ) 
        [size] => Array ( 
            [1] => 0 
            [2] => 0 
        ) 
    ) 
)

Now the problem is that I want to use wordpress' upload handler, wp_handle_upload. It expects the $_FILES array as an argument, but only with one file. I guess it can only be two arrays deep, not three as mine is. So I'm wondering if there's a way to submit the files one at a time from the $_FILES array. The files have the same key in each array.

EDIT: Changed the post since I learned that the wp_handle_upload wants the $_FILES array as argument.

1
  • Do your file inputs happen to carry a name= like image[]? Commented Nov 14, 2010 at 18:22

3 Answers 3

22

Try:

$files = $_FILES['image'];
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]
    );
    wp_handle_upload($file);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This got me almost all the way, thank you! I had to change $key in the array to $x, because thats the variable used to increment. Now the only problem is that when I leave a filefield empty it still gets processed through that loop.
@Toxid: See my update, you can just wrap the whole thing in an if.
Thank you very much! I was experimenting with ifs but couldnt figure out how to do it.
@Toxid if($_FILES['image']['error']<1) { //do something }
0

I tried this and it worked for me

foreach ($course_video_files['name'] as $key => $value):
                if ($course_video_files['name'][$key]) {
                    $file = array(
                        'name'     => $course_video_files['name'][$key],
                        'type'     => $course_video_files['type'][$key],
                        'tmp_name' => $course_video_files['tmp_name'][$key],
                        'error'    => $course_video_files['error'][$key],
                        'size'     => $course_video_files['size'][$key]
                    );

                    $upload_video_id = MPR_Core::upload_media($file);
                    if( is_int($upload_video_id) ):
                      // if upload is success do something with attachment id
                   
                    endif;
                }
 endforeach;

and use this function for uploading

function upload_media($file, $post_id = null){
    // handle video upload
    $uploadedfile = $file;
    $upload_overrides = array(
        'test_form' => false
    );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    if ( $movefile && ! isset( $movefile['error'] ) ) {

        if( is_array($movefile) && !empty($movefile) ):

            // $filename should be the path to a file in the upload directory.
            $filename = $movefile['file'];
            //$filename = '/path/to/uploads/2013/03/filename.jpg';

            // The ID of the post this attachment is for.
            $parent_post_id = $post_id;

            // Check the type of file. We'll use this as the 'post_mime_type'.
            $filetype = wp_check_filetype( basename( $filename ), null );

            // Get the path to the upload directory.
            $wp_upload_dir = wp_upload_dir();

            // Prepare an array of post data for the attachment.
            $attachment = array(
                'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
                'post_mime_type' => $filetype['type'],
                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );

            // Insert the attachment.
            $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

            // Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            set_post_thumbnail( $parent_post_id, $attach_id );

            return $attach_id;
        else:
            return false;
        endif;


    } else {
        /*
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
        //echo $movefile['error'];
        return $movefile;
    }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

Couldn't you loop through your files array and then call the upload_handlers?

e.g.

for( $i = 1; $i <= count( $_FILES['image']['name']; $i++ ) {
    // just cguessing on the args wp_handle_upload takes
    wp_handle_upload( $_FILES['images']['tmp'][$i], $_FILES['images']['name'][$i] );
}

1 Comment

Good idea, I've thought about this too. After doing a bit of research, I realize I was wrong in my first post. The wp_handle_upload expects the $_FILES array as argument, but only with one file. So I guess it can only be two arrays deep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.