1
votes

Since TinyMCE 4.x, TinyMCE has an build in image upload, but I can't get it to work.

My code:

tinymce.init({    
            selector: "textarea",
            plugins: [
                "image","code","autoresize","media","link","imagetools"
            ],
            link_class_list: [
                {title: 'None', value: ''},
                {title: 'PDF', value: 'pdf'}
            ],
            default_link_target: "_blank",
            images_upload_url: "uploadImage.php",
            toolbar1: "insertfile undo | redo | styleselect | bold | italic | alignleft | aligncenter | alignright | alignjustify | bullist | numlist | outdent | indent | link | image | media | code",
            toolbar2: "",
            menubar : false,
            statusbar : false,
            convert_urls: false,
            content_css: 'editor-style.css',
            width: 800
        });

The line: "images_upload_url" shoud be responsible for the image upload. The php file attached to this is the following:

 $imageFolder = "../images/tinymce";

  reset ($_FILES);
  $temp = current($_FILES);
  if (is_uploaded_file($temp['tmp_name'])){
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
    }


    if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
        header("HTTP/1.0 500 Invalid file name.");
        return;
    }


    if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
        header("HTTP/1.0 500 Invalid extension.");
        return;
    }

    $filetowrite = $imageFolder . $temp['name'];
    move_uploaded_file($temp['tmp_name'], $filetowrite);

    echo json_encode(array('location' => $filetowrite));
  } else {
    header("HTTP/1.0 500 Server Error");
  }

Am I missing something? Where can I find the "upload image" button in the editor (I expect it to appear in the "Insert/edit image" window). Is there a working example somewhere?

1
Hi Jeroen, I too am having this problem (no upload icon). I believe TinyMCE has this ability since 4.2, not 4.x. Did you ever get your working? If so, would you mind posting an answer describing the solution? - Swisher Sweet
Hi Swisher Sweet, no I haven't got it working, I created a seperate media library where users can upload files, that works fine for now but I want to dive some deeper in it later. - Jeroen

1 Answers

2
votes

i use tinymce 4 here is how i do image upload :

timymce config :

file_browser_callback: function(field_name, url, type, win) {
if(type=='image') $('#file_name').click(); $('#field_name').val(field_name);
}

see : https://www.tinymce.com/docs/configure/file-image-upload/

form :

<form enctype="multipart/form-data" id="form_file" style="width:0px; height:0px; overflow:hidden">
<input id="file_name" name="file" type="file" />
</form>
<input type="hidden" id="field_name" value="" />

i use the input hidden field_name to display the image link in the popup window

javascript :

$('#file_name').change(function(){
var field_name = $('#field_name').val()
document.getElementById(field_name).value='';
var file = this.files[0];
var name = file.name;
var size = file.size;
var type = file.type;
//Your validation
var field_name = $('#field_name').val()
var type = type.substring(0, 5);
if(type=='image') { 

    var formData = new FormData($('#form_file')[0]);

    $.ajax({
        url: "upload/upload.php",
        type: "POST",
        data: formData,
        async: false,
        success: function (msg) {
            document.getElementById(field_name).value = 'http://www.domain.fr/upload/'+name;        
        },
        cache: false,
        contentType: false,
        processData: false
    });

} else {
    alert('Le fichier doit etre une image') 
}

});

php :

header('Access-Control-Allow-Origin: *');


if($_FILES['file']['type'] != "") {
    $source = $_FILES['file']['name'];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    if(move_uploaded_file ($_FILES['file']['tmp_name'], $source)) { 
        echo $source ;
    }   
}