Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Uploading Image with TinyMCE current($_FILES); returns false Codeigniter 4

$
0
0

I'm trying to implement uploading images with TinyMCE. I did it on another website, which was on Codeigniter 3 and now I'm implementing it with CodeIgniter 4. For some reason, calling current($_FILES); after choosing the image is returning FALSE on this implementation. I'm using TinyMCE 6.8.2 on both. Any ideas on how to troubleshoot is appreciated!

I'm following this tutorial for the image handler: https://www.tiny.cloud/docs/advanced/php-upload-handler/

I get "Trying to access array offset on value of type bool" on is_uploaded_file($temp['tmp_name']

My TinyMCE init:

       tinymce.init({            selector: "textarea",            statusbar:false,            height: 450,            relative_urls:false,            browser_spellcheck:true,            content_css:'<?php echo base_url("css/desktop.css")?>',            plugins: ["link","image","code","fullscreen"],                      menubar: true,            toolbar: "bold italic underline strikethrough | fontsizeselect | alignleft aligncenter alignright alignjustify | styleselect | link | paste | bullist numlist | outdent indent | blockquote | removeformat | image caption | code",            paste_data_images:false,              /* enable automatic uploads of images represented by blob or data URIs*/            automatic_uploads: true,            images_upload_url: "<?php echo site_url("manage/tinyMCE_upload/");?>",            file_picker_types: 'image',            file_picker_callback: (cb, value, meta) => {                const input = document.createElement('input');                input.setAttribute('type', 'file');                input.setAttribute('accept', 'image/*');                input.addEventListener('change', (e) => {                  const file = e.target.files[0];                  const reader = new FileReader();                  reader.addEventListener('load', () => {                    /*                      Note: Now we need to register the blob in TinyMCEs image blob                      registry. In the next release this part hopefully won't be                      necessary, as we are looking to handle it internally.                    */                    const id = file.name.replace(/\.[^/.]+$/, "") + (new Date()).getTime();                    const blobCache =  tinymce.activeEditor.editorUpload.blobCache;                    const base64 = reader.result.split(',')[1];                    const blobInfo = blobCache.create(id, file, base64);                    blobCache.add(blobInfo);                    /* call the callback and populate the Title field with the file name */                    cb(blobInfo.blobUri(), { title: file.name });                  });                  reader.readAsDataURL(file);                });                input.click();              },             remove_script_host : false        });

From the tutorial, in my controller. Line 14 is erroring out.

Edit: I logged out current($_FILES); and it is coming back false. I'm not sure why that would be? Any ideas?

    public function tinyMCE_upload() {          if (isset($_SERVER['HTTP_ORIGIN'])) {          header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);      }      // Don't attempt to process the upload on an OPTIONS request      if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {        header("Access-Control-Allow-Methods: POST, OPTIONS");        return;      }      reset ($_FILES);      $temp = current($_FILES);      if (is_uploaded_file($temp['tmp_name'])){        // Sanitize input        if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {            header("HTTP/1.1 400 Invalid file name.");            return;        }        // Verify extension        if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {            header("HTTP/1.1 400 Invalid extension.");            return;        }        $filetowrite = $temp['name'];        // Determine the base URL        $baseurl = site_url('images/');        if (move_uploaded_file($temp['tmp_name'], 'images/'.$filetowrite)) {                echo json_encode(array('location' => $baseurl.$filetowrite));        }        else {            header("HTTP/1.1 500 Server Error");        }      } else {        // Notify editor that the upload failed        header("HTTP/1.1 500 Server Error");      } }

Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>