- Getting Started
- Browser Support
- Languages Support
- Shortcuts
- Activation
- Examples
- Customize the Editor
- Use-cases
- Plugins
- APIs
- Development Frameworks
- Server Integrations
- Server SDKs
- Migration Guides
- Changelog
- Tutorials
PHP Image Server Upload
How it works
- Specify upload options when initializing the editor.
- On image insertion, the editor automatically makes an AJAX request to the server.
- Once the request reaches the server, it stores the image and sends back to the client the link to the uploaded image.
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);
}
?>
Additional Resources
- Building a Support System Using Laravel PHP Framework and Froala HTML Editor Software – Part 1
- Building a Support System Using Laravel PHP Framework and Froala HTML Editor Software – Part 2
- Building a Support System Using Laravel PHP Framework and Froala HTML Editor Software – Part 3
- How To Use WYSIWYG Editor In PHP
Do you think we can improve this article? Let us know.