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 );
            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

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to upload two input files

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.