New Froala Editor v5.5.0 is here – Learn More
Froala Documentation
- Installation Guides
- Browser Support
- Languages Support
- Shortcuts
- Activation
- Examples
- Customize the Editor
- Use-cases
- Plugins
- APIs
- Development Frameworks
- Server Integrations
- Server SDKs
- Migration Guides
- Changelog
- Tutorials
- Froala Docs
- /
- SDKs
- /
- PHP
- /
- Image Server Delete
PHP Image Server Delete
How it works
- Code intercepts the image removed or replaced in the editor.
- Removing an image sends to the server a request to delete the image from disk.
- Once the request reaches the server, it deletes the image.
Intercept image being removed
Removing an image from the editor triggers two events: image.beforeRemove and image.removed. You can use either of them to delete the image from the server, however it is recommended to use the second one, since at that point you make sure that the image was removed from the editable area.
Replacing an image triggers image.replaced. Starting with v5.5, it passes the previous image's src as a third argument, old_src, so you can delete the replaced file the same way.
Send request to the server
There is no built-in feature that makes the request to the server, however you can use an AJAX request.
<script>
// Ask the server to delete an image file.
function deleteImage (src) {
fetch('/delete_image.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ src: src })
})
.then(function (response) {
console.log(response.ok ? 'Image was deleted' : 'Image delete problem');
})
.catch(function (err) {
console.log('Image delete problem: ' + err);
});
}
new FroalaEditor('.selector', {
events: {
// An image was removed from the editor.
'image.removed': function ($img) {
deleteImage($img.attr('src'));
},
// An image was replaced. old_src is the image that was replaced (v5.5+).
'image.replaced': function ($img, response, old_src) {
deleteImage(old_src);
}
}
});
</script> Delete the image
The server implementation is responsible for receiving the request and handling it appropriately. The code on the previous step makes the image path available in the $_POST['src'] variable. The FroalaEditor_Image::delete() method from the PHP SDK is expecting the path of the image to remove from disk.
Note: The path of the image is relative to the $_SERVER['DOCUMENT_ROOT'] global variable.
$response = FroalaEditor_Image::delete($_POST['src']);
Complete Example
<script>
// Ask the server to delete an image file.
function deleteImage (src) {
fetch('/delete_image.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ src: src })
})
.then(function (response) {
console.log(response.ok ? 'Image was deleted' : 'Image delete problem');
})
.catch(function (err) {
console.log('Image delete problem: ' + err);
});
}
new FroalaEditor('.selector', {
events: {
// An image was removed from the editor.
'image.removed': function ($img) {
deleteImage($img.attr('src'));
},
// An image was replaced. old_src is the image that was replaced (v5.5+).
'image.replaced': function ($img, response, old_src) {
deleteImage(old_src);
}
}
});
</script> <?php
// Include the editor SDK.
require 'PATH_TO_FROALA_SDK/lib/froala_editor.php';
// Delete the image.
try {
$response = FroalaEditor_Image::delete($_POST['src']);
echo stripslashes(json_encode('Success'));
} 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.