Search code examples
phpajaxwordpressbase64file-put-contents

base64_decode image or pdf is failing in PHP when size is higher than 500kb


I'm uploading files (image or pdf) using AJAX. My process is converting the file to base64 then send the data via AJAX then process in server side (PHP) to become a image or pdf. This is my code in server side and it's working fine but failing when file size is above 500kb.

if ($picture_ext == 'pdf') { //pdf                      
    $image_generated_name = $select_name . '_' . $generate_rand_num . '_file.pdf';                      
    file_put_contents(WP_PLUGIN_DIR.'/plugin_name/uploads/'.$image_generated_name, base64_decode(substr($product_img_upload,28)));
} else { //image    
    file_put_contents(WP_PLUGIN_DIR.'/plugin_name/uploads/'.$image_generated_name, base64_decode(substr($product_img_upload,22)));  
}

Solution

  • We dont know what the error is, but I suspect its possibly with your in variables post_max_size and upload_max_filesize. You can modifiy these in php.ini config, Add the following commands before that is run, see if it works, and modify your ini based on that:

    ini_set('post_max_size', '10M');
    ini_set('upload_max_filesize', '10M');