PHP ckeditor Image Upload implementation code

Source: Internet
Author: User

I spent an afternoon writing a script for processing uploaded files using a PHP script. Code , No more security measures, and hope to be useful to everyone.
First, add the following code to your Config. js file: Copy 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 file to be processed. You can modify it as needed. The upload. php file is as follows: Copy code The Code is as follows: <? PHP
/*
Ckeditor_upload.php
Monkee

*/
$ 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 allows suffixes
$ Config ['Flash _ size'] = 200; // The maximum size of the uploaded flash is kb.
$ Config ['img _ size'] = 500; // the maximum size of uploaded IMG is kb.
$ Config ['message'] = "uploaded successfully"; // The message displayed after the upload is successful. If it is null, the message is not displayed.
$ Config ['name'] = mktime (); // the uploaded file naming rules are named in UNIX timestamps.
$ Config ['Flash _ dir'] = "/ckeditor/upload/flash"; // upload the Flash file address with an absolute address to facilitate upload. "/" is not added after the PHP file is placed in any location on the site "/"
$ Config ['img _ dir'] = "/ckeditor/upload/IMG"; // upload the imgfile address with an absolute address and an absolute address to facilitate upload. "/" is not added after the PHP file is placed in any location on the site "/"
$ Config ['site _ url'] = ""; // the website address. This parameter is related to the Image Upload address. "/" is left blank.
// File Upload
Uploadfile ();
Function uploadfile ()
{
Global $ config;
// Determine whether 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, "", "incorrect file call request ");
$ Type = $ _ Get ['type'];
If (is_uploaded_file ($ _ FILES ['upload'] ['tmp _ name'])
{
// Determine whether a file is allowed to be uploaded
$ Filearr = pathinfo ($ _ FILES ['upload'] ['name']);
$ Filetype = $ filearr ["extension"];
If (! In_array ($ filetype, $ config [$ type])
Mkhtml ($ FN, "", "incorrect file type! ");
// Determine 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/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 );
}
?>

Package and download code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.