3

I'm trying to upload images using Froala wysiwyg editor to my localhost (for testing purposes) but it's not working. When I choose the image to upload, it appears faded in the editor, then disappears when I click somewhere else. Here's my code below and a link to their documentation I tried to follow as well.

Documentation: https://www.froala.com/wysiwyg-editor/docs/concepts/image/upload

There's also this method, but I'm not sure where to put the php: https://www.froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload

MY CODE

("fedit" is the class I use for my textarea.)

Javascript:

<script>
  $(function() {
    $('.fedit').froalaEditor({
        // Set the image upload parameter.
        imageUploadParam: 'file',

        // Set the image upload URL.
        imageUploadURL: '/upload_image.php',

        // Additional upload params.
        imageUploadParams: {class: 'fedit'},

        // Set request type.
        imageUploadMethod: 'POST',

        // Set max image size to 5MB.
        imageMaxSize: 5 * 1024 * 1024,

        // Allow to upload PNG and JPG.
        imageAllowedTypes: ['jpeg', 'jpg', 'png']
      })
      .on('froalaEditor.image.beforeUpload', function (e, editor, images) {
        // Return false if you want to stop the image upload.
      })
      .on('froalaEditor.image.uploaded', function (e, editor, response) {
        // Image was uploaded to the server.
      })
      .on('froalaEditor.image.inserted', function (e, editor, $img, response) {
        // Image was inserted in the editor.
      })
      .on('froalaEditor.image.replaced', function (e, editor, $img, response) {
        // Image was replaced in the editor.
      })
      .on('froalaEditor.image.error', function (e, editor, error, response) {
        // Bad link.
        else if (error.code == 1) { ... }

        // No link in upload response.
        else if (error.code == 2) { ... }

        // Error during image upload.
        else if (error.code == 3) { ... }

        // Parsing response failed.
        else if (error.code == 4) { ... }

        // Image too text-large.
        else if (error.code == 5) { ... }

        // Invalid image type.
        else if (error.code == 6) { ... }

        // Image can be uploaded only to same domain in IE 8 and IE 9.
        else if (error.code == 7) { ... }

        // Response contains the original server response to the request if available.
      });
  });
</script>

upload_image.php

<?php
    // Allowed extentions.
    $allowedExts = array("gif", "jpeg", "jpg", "png", "blob");

    // Get filename.
    $temp = explode(".", $_FILES["file"]["name"]);

    // Get extension.
    $extension = end($temp);

    // An image check is being done in the editor but it is best to
    // check that again on the server side.
    // Do not use $_FILES["file"]["type"] as it can be easily forged.
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);

    if ((($mime == "image/gif")
    || ($mime == "image/jpeg")
    || ($mime == "image/pjpeg")
    || ($mime == "image/x-png")
    || ($mime == "image/png"))
    && in_array(strtolower($extension), $allowedExts)) {
        // Generate new random name.
        $name = sha1(microtime()) . "." . $extension;

        // Save file in the uploads folder.
        move_uploaded_file($_FILES["file"]["tmp_name"], getcwd() . "/uploads/ " . $name);

        // Generate response.
        $response = new StdClass;
        $response->link = "/uploads/" . $name;
        echo stripslashes(json_encode($response));
    }
?>
KeepCool
  • 497
  • 1
  • 6
  • 24
  • not enough information to go on here, does you JS console give you any information or how about your error log on your server. – cmorrissey Nov 29 '16 at 19:16
  • Thanks for the response. I'm getting an error in my log: Failed to load resource: the server responded with a status of 404 (Not Found)http://localhost/froala_uploads/ . Then another error when I try to add the image: image.min.js:7 GET http://localhost/froala_uploads/ 404 (Not Found)f @ image.min.js:7dispatch @ jquery.js:3r.handle @ jquery.js:3 . Btw, I changed my folder to "froala_uploads", just for easier identifying. – KeepCool Nov 29 '16 at 19:43
  • They offer a SDK on their site, why are you not using that? – cmorrissey Nov 29 '16 at 19:58
  • Unfortunately, I'm really not that advanced in programming. I know php pretty well, but never quite understood what an SDK is. Is that something you use instead of the "upload_image.php, or along with it? https://www.froala.com/wysiwyg-editor/docs/sdks/php. – KeepCool Nov 29 '16 at 20:31
  • https://www.froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload I'm not sure if I should put the php code here in a seperate file called upload_image.php, or should that file still contain the code above? – KeepCool Nov 29 '16 at 20:46
  • I got it working! I will update post or answer my own question to tell everyone how I did it for people who were having issues like me. Thanks cmorrissey for letting me know I should look more into the sdk. – KeepCool Nov 29 '16 at 20:58

1 Answers1

1

Here is the solution to making it work on your localhost using the SDK for php. (I am using WAMP). Thanks to cmorrissey in the comments above for making me look into the sdk.

Download the SDK files here: https://www.froala.com/wysiwyg-editor/docs/sdks/php

Follow these instructions: https://www.froala.com/wysiwyg-editor/docs/sdks/php/image-server-upload

But I will show you the code and mention a few points that were the issue for me.

Put this javascript at the bottom of the page where your textarea is:

<script>
  $(function() {
    $('.selector').froalaEditor({
      // Set the image upload URL.
      imageUploadURL: '/mywebsite/upload_image.php'
    })
  });
</script>

Please note that your "imageUploadURL:" (upload_image.php) path points to the root of your local server. Eg. wherever "http://localhost" points to. So in my case I had my website in a subfolder of the root, so I would put "/mywebsite/upload_image.php" (which is actually located in C:\wamp64\www\mywebsite).

In the upload_image.php file:

<?php

// Include the editor SDK.
require 'froala/sdk/lib/FroalaEditor.php';

// Store the image.
try {
  $response = FroalaEditor_Image::upload('/mywebsite/img/uploads/');
  echo stripslashes(json_encode($response));
}
catch (Exception $e) {
  http_response_code(404);
}

?>

As you can see I copied the sdk "lib" folder (you downloaded above) toa subfolder I created called "froala/sdk/". You can put it wherever you want.

Another important quick note is a typo they did. In their example php code, they tell you the file is called "froala_editor.php", when it actually is named "FroalaEditor.php" in the downloaded files. This was one of the issues I had.

Everything should work now. You'll just have to modify the paths when you upload it to your webserver.

KeepCool
  • 497
  • 1
  • 6
  • 24
  • I get the same problem, testing in my local. I access a local file and try to show on the editor, the same issue happens. But I am using typescript and angular 2. Could you please help? – Edgar Aug 18 '17 at 09:53