• Resolved Blindacme

    (@blindacme)


    I know this is probably super easy and I am overlooking something here.

    I have a form on the front-end of our website where users can upload an image via a form and it creates a product in our shop. We are using WordPress and Woocommerce but I felt this question was general enough to ask on here.

    Currently everything works fine when uploading one image, How would I make this work for when a user uploads multiple images?

    In the form I changed
    <input type="file" name="thumbnail" id="thumbnail" />
    to
    <input type="file" name="thumbnail" id="thumbnail" multiple />and I thought wrapping the below in something like this would work:

    if ($_FILES) {
          foreach ($_FILES as $file => $array) {
            // do necessary code
          }
        }

    But it doesn’t.

    Here is what I have that creates the post. Im not sure why wrapping it in the foreach $FILES won’t work?

    You can view the paste bin here: https://pastebin.com/VvwBAEG5

    In short I just need a way for the above code to create the post for each file uploaded, instead of just one file uploaded like it currently does.. Im all out of ideas

Viewing 1 replies (of 1 total)
  • Thread Starter Blindacme

    (@blindacme)

    Got it, so I changed the name of the file input from thumbnail to thumbnail[]. Then I wrapped the above paste in a function and added this part at the top of the file and it works perfectly:

    if ( $_FILES ) {
        	$files = $_FILES['thumbnail'];
        		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]
        		  );
    
        			  $_FILES = array("" => $file);
    
        			  foreach ($_FILES as $file => $array) {
        			          //call my function
        				  create_var_product($file, $post_id);
        			  }
        		  }
        	}
        }
Viewing 1 replies (of 1 total)
  • The topic ‘foreach $FILES create post how to’ is closed to new replies.