Last active
August 20, 2019 10:25
-
-
Save mazfreelance/04158d334ccb4b638c5f2d013b1af741 to your computer and use it in GitHub Desktop.
PHP - Summernote library: Store uploaded image file into folder server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$file_name = str_replace(base_url().'/uploads/email/', '', $src); // striping host to get relative path | |
if(unlink($file_name)) | |
{ | |
echo 'File Delete Successfully'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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