Skip to content

Instantly share code, notes, and snippets.

@mazfreelance
Last active August 20, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazfreelance/04158d334ccb4b638c5f2d013b1af741 to your computer and use it in GitHub Desktop.
Save mazfreelance/04158d334ccb4b638c5f2d013b1af741 to your computer and use it in GitHub Desktop.
PHP - Summernote library: Store uploaded image file into folder server.
$file_name = str_replace(base_url().'/uploads/email/', '', $src); // striping host to get relative path
if(unlink($file_name))
{
echo 'File Delete Successfully';
}
$('#summernote').summernote({
height: 500,
tabsize: 4,
callbacks: {
onImageUpload: function(files, editor, editable){
sendFile(files[0], editor, editable)
},
onMediaDelete : function(target) {
deleteFile(target[0].src);
}
}
});
function sendFile(file, editor, welEditable){
data = new FormData();
data.append('file', file);
$.ajax({
url: url, // base_url/upload.php
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
$('#summernote-message').summernote('insertImage', data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log(textStatus+' '+errorThrown);
}
});
}
function deleteFile(src) {
$.ajax({
data: {src : src},
type: 'POST',
url: url, // base_url/deleteimagepost.php
cache: false,
success: function(resp) {
console.log(resp);
}
});
}
if ($_FILES['file']['name']) {
if (!$_FILES['file']['error']) {
$filename = $_FILES['file']['name'];
$destination = $filePath.'/' . $filename;
$location = $_FILES["file"]["tmp_name"];
move_uploaded_file($location, $destination);
echo base_url().'/uploads/email/'.$filename;
}
else
{
echo $message = 'Upload Fail: '.$_FILES['file']['error'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment