0

I have a tinymce code that will be insert into the mysql database with the ID taking from the last id input using insert_id() to another function and when giving the value to the id an error will appear "Failed to upload image: HTTP Error: 500", can you guys help me ?

this is my controller :

function save()
{
    $judul = $this->input->post('judul');
    $isi = $this->input->post('isi');
    $kategori = implode(',', $this->input->post('kategori'));
    $c_date=date("Y-m-d H:i:s");

    $data = array(
        'judul' => $judul,
        'isi' => $isi,
        'kategori' => $kategori,
        'publish' => $c_date
    );

    $ids = $this->text_editor_model->simpan($data);
    $this->upload_image($ids);
    redirect('text_editor');
}

function upload_image($ids)
{
    $config['upload_path'] = './berkas/news/';
    $config['allowed_types'] = 'jpg|png|jpeg';
    //$config['max_size'] = 0;

    $this->load->library('upload', $config);

    if ( !$this->upload->do_upload('file')) {
        $this->output->set_header('HTTP/1.0 500 Server Error');
        exit;
    } else {
        $file = $this->upload->data();
        $this->output
            ->set_content_type('application/json', 'utf-8')
            ->set_output(json_encode(['location' => base_url().'/berkas/news/'.$file['file_name']]))
            ->_display();
        $count = count($file['file_name']);
            for ($i=0; $i < $count ; $i++) {
                $data2[$i]['id_berita'] = $ids; //error in here
                $data2[$i]['img_name'] =  $file['file_name'];
            }
            $this->text_editor_model->insert_img($data2);
            exit;
    }
}

and this is my tinymce script :

tinymce.init({
    selector: "#editor",
    height: 400,
    plugins: [
         "advlist autolink lists link image charmap print preview hr anchor pagebreak",
         "searchreplace wordcount visualblocks visualchars code fullscreen",
         "insertdatetime nonbreaking save table contextmenu directionality",
         "emoticons template paste textcolor colorpicker textpattern"
    ],
    toolbar: "insertfile undo redo | styleselect | fontsizeselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image responsivefilemanager table",
    images_upload_url: "<?php echo site_url('text_editor/upload_image')?>",
    automatic_uploads: true,
    image_advtab : true,
    file_picker_types: 'image', 
    relative_urls: false,
    remove_script_host: false,
    image_dimensions: false,
    image_class_list: [
        {title: 'Responsive', value:'img-responsive'}
      ],
    file_picker_callback: function(cb, value, meta) {
       var input = document.createElement('input');
       input.setAttribute('type', 'file');
       input.setAttribute('accept', 'image/*');
       input.onchange = function() {
          var file = this.files[0];
          var reader = new FileReader();
          reader.readAsDataURL(file);
          reader.onload = function () {
             var id = 'blog-' + (new Date()).getTime();
             var blobCache =  tinymce.activeEditor.editorUpload.blobCache;
             var blobInfo = blobCache.create(id, file, reader.result);
             blobCache.add(blobInfo);
             cb(blobInfo.blobUri(), { title: file.name });
          };
       };
       input.click();
    }
});
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • did you try to debug it with `var_dump($_FILES)` in your function? – M.Hemant May 29 '19 at 03:35
  • yes, the result is 'array(0) { }' – yopie alamsyah May 29 '19 at 03:42
  • now check your console->network tab to check your ajax is sending your `file` or not – M.Hemant May 29 '19 at 03:44
  • i got error in function upload_image() but if i change $data2[$i]['id_berita'] = $ids; to $data2[$i]['id_berita'] = 1; or another int value everything works, why ? – yopie alamsyah May 29 '19 at 03:58
  • because `$ids` is blank – M.Hemant May 29 '19 at 04:10
  • when i try debug with var_dump($ids) is contain value not blank – yopie alamsyah May 29 '19 at 04:32
  • `$ids` is single id or array? – M.Hemant May 29 '19 at 04:37
  • $ids is single id – yopie alamsyah May 29 '19 at 04:56
  • how about instead of `$this->output->set_header('HTTP/1.0 500 Server Error');` you actually `echo $this->upload->display_errors()` to see what is going on?. further if you are uploading a single file why are you doing a `for` loop after uploading? – Alex May 29 '19 at 05:03
  • because I want to multiple post image in one single input – yopie alamsyah May 29 '19 at 06:19
  • now I find the problem, so if I set '$ids' in the 'upload_image ($ ids = " ")' function parameter to null, everything works fine but if I don't set it to null or containing value 'upload_image($ids)', the error notification reappears – yopie alamsyah May 29 '19 at 06:27
  • `do_upload` only works for **one** image. this code will not work for more than 1 image (well it might, but only 1 image out of n would get uploaded and stored but I'm not sure if tinymce even sends multiple images in 1 request?). also when your controller requires a parameter i do this `function upload_image($ids = null)` because it will give you an error otherwise and then you can check if the value is null and show error if it is null because you need it for next functions. this still won't solve multiple image uploads in 1 request. – Alex May 29 '19 at 07:43
  • Possible duplicate of [Multiple image upload with CodeIgniter](https://stackoverflow.com/questions/40778683/multiple-image-upload-with-codeigniter) – Alex May 29 '19 at 07:44

0 Answers0