Days
Hours
Minutes
Seconds
x
Skip to content

PHP Image Server Upload

How it works

  1. Specify upload options when initializing the editor.
  2. On image insertion, the editor automatically makes an AJAX request to the server.
  3. Once the request reaches the server, it stores the image and sends back to the client the link to the uploaded image.

Jump to Complete Example

Initialize the editor

First, add the imageUploadURL option, as its value enter the upload destination for the images.

Next, set any additional options to configure upload methods and allowed file types: imageUploadParam, imageUploadParams, imageUploadMethod, imageMaxSize, imageAllowedTypes.

<script>
  new FroalaEditor('.selector', {
    // 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 detects the uploaded image automatically but you 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/');

To store the uploaded image, the server needs write rights on the uploads folder. Additionally, check uploaded images are publicly accessible in the browser.

Return the path to the uploaded image

If the save action is successful, the SDK generates an ImageResponse object with the absolute path to the uploaded image and the server returns the path to the client side.

echo stripslashes(json_encode($response));

Complete Example

<script>
  new FroalaEditor('.selector', {
    // 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);
}

?>

Do you think we can improve this article? Let us know.