微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在ckeditor 5中接收上传的图像文件?

如何解决如何在ckeditor 5中接收上传的图像文件?

我创建了一个上载脚本来接收使用CKeditor 5简单上载适配器构建发送给它的文件。我已经使用标准的上传文件类型对其进行了测试,并使用了我适应的文件类型字段中的$ _POST,它会生成正确的json响应供编辑者接收。

但是,我不确定如何真正地检测编辑器本身的输入,因为它不是来自输入字段。

<?PHP

header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Methods: PUT,GET,POST");

$uploadOk = 1;
$err = 0;

//getting the file
$mentionFilename = basename($_FILES["filetoUpload"]["name"]);
if ($mentionFilename == ""){
    $err = 1;
}else{

//setting the uploaddir
$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["filetoUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["filetoUpload"]["tmp_name"]);
    if($check !== false) {
        //echo "File is an image - " . $check["mime"] . ".<BR>";
        $uploadOk = 1;
    } else {
        //echo "File is not an image.<BR>";
        $err = 2;
        $uploadOk = 0;
    }
}

// Check if file already exists
if (file_exists($target_file)) {
    $err = 3;
    $uploadOk = 0;
}

// Check file size
if ($_FILES["filetoUpload"]["size"] > 5000000) {
    //echo "Sorry,your file is too large.<BR>";
    $err = 4;
    $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "pdf" ) {
    //echo "Sorry,only JPG,JPEG,PNG,GIF & PDF files are allowed.<BR>";
    $err = 5;
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk > 0) {
    if (move_uploaded_file($_FILES["filetoUpload"]["tmp_name"],$target_file)) {
        //echo "The file ". basename( $_FILES["filetoUpload"]["name"]). " has been uploaded.<BR>";
        
    } else {
        //echo "Sorry,there was an error uploading your file.<BR>";
        $err = 6;
        $uploadOk = 0;
    }
} 
}


switch ($err){
    case "1":
        //no fileneame in submissions
        $errMsg = "There appeared to be no filename in the submission";
        break;
    case "2":
        //the file is not an image
        $errMsg = "The file submitted was not a permitted image type.";
    case "3":
        //file exists
        $errMsg = "The file already exists.";
        break;
    case "4":
        //file is too large
        $errMsg = "The filesize of the upload is too large.";
    case "5":
        //Wrong filetype
        $errMsg = "The file had the wrong filetype";
    case "6":
        //Problem uploading file
        $errMsg = "There was a problem uploading the file.";
    default:
        //file was uplaoded succesfully
        
        break;
}

if ($uploadOk > 0){
    //return success message
    $returnArr = array(
        "url" => $target_file
    );
    
}else{
    //return error message
    $returnArr = array(
        "error" => array(
            "message" => $errMsg
        )
    );
}

//sending back the response
echo json_encode($returnArr);


?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。