Home  >  Article  >  Backend Development  >  PHP CKEditor upload image implementation code_PHP tutorial

PHP CKEditor upload image implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:43799browse

I spent an afternoon using PHP script to write a script code for processing uploaded files without doing any more security processing. I hope it will be useful to everyone.
First, add the following code to your config.js file:

Copy the code The code is as follows:

CKEDITOR .editorConfig = function( config )
{
config.filebrowserImageUploadUrl = './upload.php?type=img';
config.filebrowserFlashUploadUrl = './upload.php?type=flash';
};

The above configuration is the address of the uploaded file to be processed. You can modify it according to your own situation. The upload.php file is as follows:
Copy code The code is as follows:

/*
CKEditor_upload.php
monkee
2009-11-15 16:47
*/
$config=array();
$config['type']=array("flash", "img"); //Upload allowed type value
$config['img']=array("jpg","bmp","gif"); //img allowed suffix
$config['flash ']=array("flv","swf"); //Flash allows suffix
$config['flash_size']=200; //Upload flash size upper limit unit: KB
$config['img_size' ]=500; //The upper limit unit of upload img size: KB
$config['message']="Upload successful"; //The message displayed after the upload is successful, if it is empty, it will not be displayed
$config[ 'name']=mktime(); //The uploaded file naming rules are named according to the unix timestamp
$config['flash_dir']="/ckeditor/upload/flash"; //Upload flash file address The absolute address is used to facilitate the upload.php file to be placed anywhere in the site without adding "/"
$config['img_dir']="/ckeditor/upload/img"; //The upload address of the img file is used as an absolute address. The absolute address is convenient for placing the upload.php file anywhere in the site without adding "/" at the end.
$config['site_url']=""; //The URL of the website is related to the address after the image is uploaded. Do not add " at the end." /" can be left blank
//File upload
uploadfile();
function uploadfile()
{
global $config;
//Determine whether it is an illegal call
if(empty($_GET['CKEditorFuncNum']))
mkhtml(1,"","Bad function call request");
$fn=$_GET['CKEditorFuncNum'];
if (!in_array($_GET['type'],$config['type']))
mkhtml(1,"","Bad 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! ");
//Judge whether the file size meets the requirements
if($_FILES['upload']['size']>$config[$type."_size"]*1024)
mkhtml ($fn,"","The uploaded file cannot exceed ".$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 the upload directory settings and directory read and write permissions");
}
}
}
//Output js call
function mkhtml($fn,$fileurl,$message)
{
$str='';
exit($str);
}
?>

Code package download

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320878.htmlTechArticleI spent an afternoon writing a script code for processing uploaded files using PHP script, but I didn’t do it More safe handling, hope it is useful to everyone. First, in your config...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn