问题描述
我创建了一个上载脚本来接收使用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 (将#修改为@)