this is my code for Ckeditor 5 and Phalcon framework.#products_desc point to textarea id.
<script>
var myEditor;
ClassicEditor
.create( document.querySelector( '#products_desc' ) ,
{
ckfinder: {
uploadUrl: 'Ckfinder/upload'
}
}
)
.then( editor => {
console.log( 'Editor was initialized', editor );
myEditor = editor;
} )
.catch( err => {
console.error( err.stack );
} );</script>
and my php controller:
<?php
use Phalcon\Mvc\Controller;
class CkfinderController extends Controller
{
public function uploadAction()
{
try {
if ($this->request->hasFiles() == true) {
$errors = []; // Store all foreseen and unforseen errors here
$fileExtensions = ['jpeg','jpg','png','gif','svg'];
$uploadDirectory = "../public/Uploads/";
$Y=date("Y");
$M=date("m");
foreach ($this->request->getUploadedFiles() as $file) {
if (in_array($file->getExtension(),$fileExtensions)) {
if($file->getSize()<2000000)
{
if (!file_exists($uploadDirectory.$Y)) {
mkdir($uploadDirectory.$Y, 0777, true);
}
if (!file_exists($uploadDirectory.$Y.'/'.$M)) {
mkdir($uploadDirectory.$Y.'/'.$M, 0777, true);
}
$namenew=md5($file->getName().time()).'.'.$file->getExtension();
$uploadDirectory .=$Y.'/'.$M.'/';
$file->moveTo($uploadDirectory.$namenew);
}
else{
$errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
}
}
else{$errors[] = "This file extension is not allowed. Please upload a JPEG ,svg,gif,,jpg,PNG file";}
if(empty($errors))
{
echo '{
"uploaded": true,
"url": "http://localhost/cms/public/Uploads/'.$Y.'/'.$M.'/'.$namenew.'"}';
}
else{
echo '{
"uploaded": false,
"error": {
"message": "could not upload this image1"
}';}
}
}
else{
echo '{
"uploaded": false,
"error": {
"message": "could not upload this image1"
}';}
}
catch (\Exception $e) {
echo '{
"uploaded": false,
"error": {
"message": "could not upload this image0"
}';
}
}
}
?>