PHP Image Server Upload

How it works

  1. You specify the upload options when the rich text editor is initialized on client side.
  2. When an image is inserted, the javascript editor automatically makes an AJAX request to the server.
  3. Once the request hits the server, it stores the image and sends back to the client the link to the uploaded image.

Jump to Complete Example

Initialize the javascript editor

To get started, the rich text editor needs to have the imageUploadURL option set as the URL where you want to have the images uploaded. Additionally, you can set other options related to the way the images are being uploaded and what data your server receives: imageUploadParam, imageUploadParams, imageUploadMethod, imageMaxSize, imageAllowedTypes.

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

Receive the uploaded image and store it

The server implementation is responsible for receiving the request and handling it appropriately. In PHP, the uploaded image is available in the $FILES global variable. The PHP editor SDK is designed to automatically detect the uploaded image, so you just have to specify the path where to store it.

Note: The path of the image is relative to the $_SERVER['DOCUMENT_ROOT'] global variable.

$response = FroalaEditor_Image::upload('/uploads/');

For the uploaded image to be stored correctly, the server should have the rights to write in the uploads folder. Additionally, you should check that the uploaded image is public accessible in browser so it can be displayed to your users.

Return the path to the uploaded image

If the save action is completed successfully, the SDK is generating an ImageResponse object with the absolute path to the uploaded image, so your server just have to return it back to the client side.

echo stripslashes(json_encode($response));

Complete Example

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

// Include the editor SDK.
require 'PATH_TO_FROALA_SDK/lib/froala_editor.php';

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

?>