0

My problem is that I cannot upload post attachments from a frontend form. I send data through AJAX to my php files containing functions. Let me be clear: everything is working great, the post is created with all informations. The only missing thing is that the images I sent are not attached to the post.

My problem should be in my php function to attach images.

My html

<input id="moreimages" type="file" name="moreimages[]" multiple >

My php function

$pid = // project id
$uid = // current user

if ( $_FILES ) { 
$files = $_FILES["moreimages"];  
foreach ($files['name'] as $key => $value) {            
            if ($files['name'][$key]) {
                $upload_overrides             = array('test_form' => false);
                $uploaded_file                = wp_handle_upload($_FILES['file'], $upload_overrides);
                $file_name_and_location       = $uploaded_file['file'];
                $file_title_for_media_library = $_FILES['file']['name'];
                $arr_file_type      = wp_check_filetype(basename($_FILES['file']['name']));
                $uploaded_file_type = $arr_file_type['type'];
                $image = array( 
                    'post_mime_type' => $uploaded_file_type,
                    'post_title' => addslashes($file_title_for_media_library),
                    'post_content' => '',
                    'post_status' => 'inherit',
                    'post_parent' => $pid,
                    'post_author' => $uid
                ); 
                $_FILES = array ("moreimages" => $image); 
                foreach ($_FILES as $images => $array) {              
                    $image_id = wp_insert_attachment($image, $file_name_and_location, $pid);
                    $attach_data = wp_generate_attachment_metadata($image_id, $file_name_and_location);
                    wp_update_attachment_metadata($image_id, $attach_data); 
                }
            } 
        } 
    }

I repeat I see in my console.log that images are correctly sent by AJAX into FormData object. I see this when I upload FIRST.jpg.

Content-Disposition: form-data; name="moreimages[]"; filename="FIRST.jpg"
Content-Type: image/jpeg

So the problem should be in the php loop for attaching images. Could you help me please? Thanks in advance!

  • please do the minimum of debugging before to ask a question. that means looking at every variable to see if the contains the expected values and writing these values in the question. look also this page : stackoverflow.com/questions/12769982/… – mmm Apr 29 '16 at 11:32
  • Use media_handle_upload() instead in foreach loop. – N00b Apr 29 '16 at 12:09
  • thanks @TwerkingN00b for your suggestion I've solved as answered. Thanks again. – Kleeia Apr 29 '16 at 13:51
  • @mmm Sorry, I won't open a discussion, I very appreciate your suggestion, I'm open to learn, but I think that my question was quite clear besides my wrong approach which I've discovered next. What I'm saying is that maybe just driving to something more practice, related to the question, could help better users who are learning like me. I'm not asking to write my code from scratch but something like "hey dude here is a similar question" or "you're missing a function"... Thanks anyway. Cheers. – Kleeia Apr 29 '16 at 14:01
0

I've actually forgotten to create the function for uploading and attaching images. I've found the correct method into another question here Uploading Multiple Attachments From Front-End With A Description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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