Using $_FILES variable into the same function for uploading images and files

I want start saying that I’m learning and I’m trying to understand the use of $_FILES and $file_handler which are making me crazy into this function to upload attachments from a frontend form.

What I’ve discovered into mine yesterday question was that using $_FILES into the same function makes it overwritten so some nice dude suggested to change this variable in something different like $whatever. Both files could be managed and uploaded then but of course this is not happening yet and only files are uploaded. Having 2 form inputs relatively named:

  • For images: name="moreimages"
  • For files: name="morefiles"

Basically I’ve arrived at this point in my php:

if ($_FILES)
    {
        // Get the upload attachment files
        $images = $_FILES['moreimages'];
        foreach ($images['name'] as $key => $value)
        {
            if ($images['name'][$key])
            {
                $image = array(
                    'name' => $images['name'][$key],
                    'type' => $images['type'][$key],
                    'tmp_name' => $images['tmp_name'][$key],
                    'error' => $images['error'][$key],
                    'size' => $images['size'][$key]
                );

                //here I've changed the $_FILES variable into something else
                $my_processed_images = array("moreimages" => $image);
                foreach ($my_processed_images as $image => $array)
                {
                    $newupload = project_images($image,$pid);
                }
            }
        }
        // Get the upload attachment files
        $files = $_FILES['morefiles'];
        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],
                    'post_mime_type' => $files['type'][$key]
                );
                $_FILES = array("morefiles" => $file);
                foreach ($_FILES as $file => $array)
                {
                    $uploadfile = project_file($file,$pid);
                }
            }
        }
    }

and my functions look like

function project_images($file_handler, $pid)
    {
        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');
        $image_id = media_handle_upload( $file_handler, $pid );
        return $image_id;
    }
function project_file($file_handler, $pid)
    {
        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');
        $file_id = media_handle_upload( $file_handler, $pid );
        update_post_meta($file_id,'is_prj_file','1');
        return $file_id;
    }

I understand that there could be a problem with my $file_handler but I don’t know how to manage it. What really is happening is that debugging the $more_images is null and not considered and $_FILES into the second loop is uploaded instead.
Can you drive me?

EDIT

My HTML form fields are like these for entire:

<input id="moreimages" accept="image/png, image/jpeg, image/gif" type="file" name="moreimages[]" >

<input id="morefiles" accept=".zip,.pdf,.rar,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.psd,.ai" type="file" name="morefiles[]" >

Furthermore My AJAX POST STATUS is OK sending these parameters for moreimages:

Content-Disposition: form-data; name="moreimages[]"; filename="Screen Shot 2016-04-05 at 16.48.10.png"
Content-Type: image/png

PNG

and these parameters for morefiles:

Content-Disposition: form-data; name="morefiles[]"; filename="articolo-slow-food-ararat.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

So I think the problem, once again, how these data are received into php.

$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

Using $_FILES variable into the same function for uploading images and 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.