Skip to content

Instantly share code, notes, and snippets.

@rongself
Created April 9, 2015 09:20
Show Gist options
  • Save rongself/30c5f2abff304f41fde6 to your computer and use it in GitHub Desktop.
Save rongself/30c5f2abff304f41fde6 to your computer and use it in GitHub Desktop.
File upload handle for CKeditor
<?php
/**
* User: Ron
* Date: 2015/4/9
* Time: 14:09
* Depend on https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/UploadHandler.php
*/
class CKeditorFileUploader extends UploadHandler {
protected $result;
/**
* @param null $handleScriptName format: http://example.com/jsupload/server/php/ckeditor-uploader-index.php
* @param bool $uploadDir format: /path/to/upload/dir/
* @param null $fileBaseUrl format: http://example.com/jsupload/server/php/files/ckeditor-upload/
*/
public function __construct($handleScriptName,$uploadDir,$fileBaseUrl)
{
$options = array(
'script_url' => $handleScriptName,
'upload_dir' => $uploadDir,
'upload_url' => $fileBaseUrl,
'delete_type' => 'delete',
'mkdir_mode' => 0755,
'param_name' => 'upload',
'image_versions' => array()
);
parent::__construct($options,false);
}
public function handle()
{
$result = $this->post(false);
$this->result = $result['upload'][0];
$callback = $_GET['CKEditorFuncNum'];
$output = <<<HTML
<html>
<body>
<script type="text/javascript">
window.parent.CKEDITOR.tools.callFunction('{$callback}', '{$this->result->url}','{$this->result->error}');
</script>
</body>
</html>
HTML;
echo $output;
}
/**
* @return string The format is http://example.com/upload/server/php/
*/
public static function getBaseUrl() {
$https = !empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'on') === 0;
return
($https ? 'https://' : 'http://').
(!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
($https && $_SERVER['SERVER_PORT'] === 443 ||
$_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment