0

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 );
            var_dump($attach_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 );
            var_dump($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

1 Answer 1

0

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

Yes, the WordPress part in that code and the other code is good.

However, if your first code is placed above/before your second code, then the problem is the following if, which applies to both the file upload inputs in your form (post_image and trabajos_empresa):

if($_FILES[$file]['error'] !== UPLOAD_ERR_OK){
    return "upload error : " . $_FILES[$file]['error'];
}

Because for the trabajos_empresa input, which is an array (because it has the multiple attribute), $_FILES['trabajos_empresa']['error'] is actually an array (of upload status, which are integers), hence $_FILES['trabajos_empresa']['error'] !== UPLOAD_ERR_OK (i.e. <array> !== <integer>) is true and therefore the return line in the above code skips uploading the files uploaded via the trabajos_empresa input.

And your second code is actually good, so try changing the first code (the whole if($_FILES) { ... } if($attach_id > 0) { ... } part) to this one:

if ( isset( $_FILES['post_image'] ) ) {
    $attach_id = media_handle_upload( 'post_image', $post_id );

    if ( $attach_id && ! is_wp_error( $attach_id ) ) {
        set_post_thumbnail( $post_id, $attach_id );
    }
}

Notes:

And despite your second code is good, you should check if $_FILES['trabajos_empresa'] is set like I did above with the other input. So for example, if ( isset( $_FILES['trabajos_empresa'] ) ) { put your second code here }. Also, if an upload didn't succeed, you could actually inspect the error message like so: echo $attachment_id->get_error_message();.

2
  • Totaly works, i just make a little adjustement to save in the multiple images their id and url to be able to show in my single page, thanks!, if i want to use this code with ajax i just have to make the classic $('#post_image')[0].files[0]; and all that stuff don't?
    – Maria
    Sep 8, 2021 at 3:35
  • Yes basically, but that's actually a JS question, so if you need further assistance, you should ask on Stack Overflow instead of here. (This question is also actually better suited on SO because the main issue was a generic PHP stuff.) :)
    – Sally CJ
    Sep 10, 2021 at 13:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.