custom post types – How to upload two input files


i’m doing a front end post submission for a CPT called “empresas” and with CMB2 i create a custom field called portfolio which type is file_list so the business can upload their work

In my custom form i have a input single file to upload the company logo and other input file multiple for upload their work

I’m using the following code to upload the logo as a featured image for post and works without any problem

if($_FILES) {
        foreach( $_FILES as $file => $array ) {
            if($_FILES[$file]['error'] !== UPLOAD_ERR_OK){
                return "upload error : " . $_FILES[$file]['error'];
            $attach_id = media_handle_upload( $file, $post_id );
    if($attach_id > 0) {
        update_post_meta( $post_id,'_thumbnail_id', $attach_id );

The problem is when i add the input multiple file and the code, it dosen’t upload any image, i tried this code doing a little modification but isn’t working

The code for upload the multiple file is

$otherfiles = $_FILES['trabajos_empresa'];
foreach ( $otherfiles['name'] as $key => $value ) {
        if ( $otherfiles['name'][ $key ] ) {
            $file = array(
                'name' => $otherfiles['name'][ $key ],
                'type' => $otherfiles['type'][ $key ],
                'tmp_name' => $otherfiles['tmp_name'][ $key ],
                'error' => $otherfiles['error'][ $key ],
                'size' => $otherfiles['size'][ $key ],
            $_FILES['trabajos_empresa'] = $file;
            $attachment_id = media_handle_upload( 'trabajos_empresa', $post_id );
            update_post_meta( $post_id,'_studiare_portafolio', $attachment_id );
            if ( is_wp_error( $attachment_id ) ) {
                // There was an error uploading the image.
                echo 'Error adding file';
            } else {
                // The image was uploaded successfully!
                //Echo to see if image upload fine
                echo 'File added successfully with ID: ' . $attachment_id . '<br>';
                echo wp_get_attachment_image( $attachment_id, array( 800, 600 ) ) . '<br>'; // Display the uploaded image with a size you wish. In this case it is 800x600

And the html im using is

 <input type="file" name="post_image" id="post_image" aria-required="true"> 

            <input type="file" name="trabajos_empresa[]" id="trabajos_empresa" aria-required="true" multiple="multiple">

if i do a var_dump i get all the files uploaded in the input trabajos_empresa but nothing is save in my media gallery

Maria 11 months 2021-09-05T16:51:20-05:00 0 Answers 0 views 0

Leave an answer