0

I added this code but when I upload 1 image, then 4 images of 1 image are uploaded, what is happening

IMAGES : Not proper working

PDF file upload : work fine

Doc file upload : work fine

rvt file upload : not working

Now I am trying to upload multiple files my HTML code is:

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

Code > file custom.php

if ( $_FILES ) { 
$files = $_FILES["my_file_upload"];  
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 ("my_file_upload" => $file); 
            foreach ($_FILES as $file => $array) {              
                $newupload = my_handle_attachment($file,$pid); 
            }
        } 
    } 
}

Function.php File code

function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
// check to make sure its a successful upload
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');

 $attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
  update_post_meta( $post_id, '_my_file_upload', $attach_id );
 }
return $attach_id;
}

enter image description here

enter image description here

7
  • 2
    For the RVT that's because WordPress restricts the set of file types you're allowed to upload. You'll need to add it to the upload_mimes filter. Do all of these files work when uploading them individually?
    – Rup
    Jan 29, 2021 at 10:45
  • @Rup thx but when I upload 1 image, then 4 images of 1 image are uploaded, what is happening
    – DK Thakur
    Jan 29, 2021 at 12:00
  • 2
    Oh, sorry, misunderstood what you were asking. One of them is your original upload, and the other four are different sizes generated by WordPress: usually there's thumbnail (150px largest dimension), medium (300px), medium large (768px) and large (1024px). If you look at the full filename it should identify the sizes it's used.
    – Rup
    Jan 29, 2021 at 12:36
  • 2
    You'll also notice that the filesize is drastically different. The second file is only 4KB, while your original is probably 80KB. You can disable this behaviour, but do so with care.
    – kero
    Jan 29, 2021 at 13:12
  • @kero thx sir one more question can yo help me
    – DK Thakur
    Jan 30, 2021 at 6:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.