前端html頁面
<script id="editor" name="content" type="text/plain" style="width:960px;height:500px;float:left;margin-top:30px;">$detail['content']</script>
<script>
//例項化編輯器 紅色內容為修改的配置
var ue = UE.getEditor('editor',{serverUrl:'<?=Url::to(['teacher/edit-upload-v1'])?>',allowDivTransToP:false});
</script>
後端
後端圖片上傳呼叫方法 TeacherController.php
/**
* UEDITOR圖片上傳功能介面
* @return array
*/
public function actionEditUploadV1()
{
$up = new UeditorUploadV1();
$result = $up->index();
return $result;
}
圖片上傳處理
UeditorUploadV1.php
public function index()
{
$action = $_GET['action'];
switch ($action) {
case 'config':
$config = $this->ueditor_config;
$config['uid'] = intval($_GET['uid']);
$result = json_encode($config);
break;
/* 上傳圖片 */
case 'uploadimage':
/* 上傳塗鴉 */
case 'uploadscrawl':
/* 上傳視訊 */
case 'uploadvideo':
/* 上傳檔案 */
case 'uploadfile':
$this->upload($action);
$result = json_encode($this->getFileInfo());
break;
}
}
/**
* 獲取當前上傳成功檔案的各項資訊
* @return array
*/
public function getFileInfo()
{
return array(
"state" => $this->stateInfo,
"url" => str_replace(Yii::$app->params['cdnpublicpath'],Yii::$app->params['cdnpublicurl'],$this->filePath),
"title" => $this->fileName,
"original" => $this->oriName,
"type" => $this->fileType,
"size" => $this->fileSize
);
}
/**
* 表單配置
* @param string $fileField 表單名稱
* @param array $config 配置項
* @param bool $base64 是否解析base64編碼,可省略。若開啟,則$fileField代表的是base64編碼的字串表單名
*/
public function upload($action)
{
$type = "upload";
switch($action){
case 'uploadimage': //匹配為圖片
$config = array(
"pathFormat" => $this->ueditor_config['imagePathFormat'],
"maxSize" => $this->ueditor_config['imageMaxSize'],
"allowFiles" => $this->ueditor_config['imageAllowFiles']
);
break;
//其他檔案型別 同上。。。
}
$this->upFile();
$this->stateMap['ERROR_TYPE_NOT_ALLOWED'] = iconv('unicode', 'utf-8', $this->stateMap['ERROR_TYPE_NOT_ALLOWED']);
}
/**
* 上傳檔案的主處理方法
* @return mixed
*/
private function upFile()
{
$file = $this->file = $_FILES[$this->fileField];
if (!$file) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
return;
}
if ($this->file['error']) {
$this->stateInfo = $this->getStateInfo($file['error']);
return;
} else if (!file_exists($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
return;
} else if (!is_uploaded_file($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
return;
}
//上傳的檔案資訊
$this->oriName = $file['name'];
$this->fileSize = $file['size'];
$this->imgType = exif_imagetype($file['tmp_name']);
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//檢查檔案大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//檢查是否不允許的檔案格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}
//建立目錄失敗
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}
//移動檔案
if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移動失敗
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
} else { //移動成功
$this->stateInfo = $this->stateMap[0];
}
}