PHP CKEditor upload image implementation code
- 2020-03-31 16:44:14
- OfStack
I spent an afternoon writing my own PHP script to handle uploading files, without doing any more security. I hope it will be useful.
First, add the following code to your config.js file:
The above configuration is the address of the file to be uploaded, which you can modify according to your own situation. The upload.php file is as follows:
(link: http://xiazai.jb51.net/200911/yuanma/ckeditor_upload.rar)
First, add the following code to your config.js file:
CKEDITOR.editorConfig = function( config )
{
config.filebrowserImageUploadUrl = './upload.php?type=img';
config.filebrowserFlashUploadUrl = './upload.php?type=flash';
};
The above configuration is the address of the file to be uploaded, which you can modify according to your own situation. The upload.php file is as follows:
<?php
$config=array();
$config['type']=array("flash","img"); //Upload the allowed type value
$config['img']=array("jpg","bmp","gif"); //Img allows suffixes
$config['flash']=array("flv","swf"); //Flash allowed suffix
$config['flash_size']=200; //Maximum flash size for upload: KB
$config['img_size']=500; //Upload img size cap unit: KB
$config['message']=" Uploaded successfully "; //The message displayed after successful upload is not displayed if it is empty
$config['name']=mktime(); //The naming convention for uploaded files is named after the Unix timestamp
$config['flash_dir']="/ckeditor/upload/flash"; //The address of the upload flash file shall be an absolute address for convenience. The upload.php file shall be placed anywhere in the station without "/" after it.
$config['img_dir']="/ckeditor/upload/img"; //Upload img file to an absolute address to make it easier to place the upload.php file anywhere in the site without adding "/"
$config['site_url']=""; //The url of the website which is related to the address of the picture after uploading can be left blank without "/" at the end
//File upload
uploadfile();
function uploadfile()
{
global $config;
//Determine if the call is illegal
if(empty($_GET['CKEditorFuncNum']))
mkhtml(1,""," Incorrect function call request ");
$fn=$_GET['CKEditorFuncNum'];
if(!in_array($_GET['type'],$config['type']))
mkhtml(1,""," Wrong file call request ");
$type=$_GET['type'];
if(is_uploaded_file($_FILES['upload']['tmp_name']))
{
//Determine whether uploading files is allowed
$filearr=pathinfo($_FILES['upload']['name']);
$filetype=$filearr["extension"];
if(!in_array($filetype,$config[$type]))
mkhtml($fn,""," Wrong file type! ");
//Determine whether the file size meets the requirements
if($_FILES['upload']['size']>$config[$type."_size"]*1024)
mkhtml($fn,""," Upload no more than one file ".$config[$type."_size"]."KB ! ");
//$filearr=explode(".",$_FILES['upload']['name']);
//$filetype=$filearr[count($filearr)-1];
$file_abso=$config[$type."_dir"]."/".$config['name'].".".$filetype;
$file_host=$_SERVER['DOCUMENT_ROOT'].$file_abso;
if(move_uploaded_file($_FILES['upload']['tmp_name'],$file_host))
{
mkhtml($fn,$config['site_url'].$file_abso,$config['message']);
}
else
{
mkhtml($fn,""," File upload failed, please check upload directory Settings and directory read and write permissions ");
}
}
}
//Output js call
function mkhtml($fn,$fileurl,$message)
{
$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', ''.$fileurl.'', ''.$message.'');</script>';
exit($str);
}
?>
(link: http://xiazai.jb51.net/200911/yuanma/ckeditor_upload.rar)