關於thinkphp5的上傳圖片和ckeditor
$upload = $_FILES['upload'];
switch($upload['error']){
case 0://說明上傳沒有什麼錯誤
break;
case 1:
case 2:
$error = '上傳檔案大小超過了最大值!'.ini_get('upload_max_filesize');
break;
case 3:
$error = '檔案只有部分被上傳!';
break;
case 4:
$error = '沒有檔案被上傳!';
break;
case 6:
$error = '找不到臨時資料夾!';
break;
case 7:
$error = '檔案寫入失敗!';
break;
default:
$error = '未知上傳錯誤!';
}
if(isset($error)){
echo "<script>window.parent.CKEDITOR.tools.callFunction($callback, '', '".$error."');</script>";exit();
}
if($upload['size']>204800){
echo "<script>window.parent.CKEDITOR.tools.callFunction($callback, '', '圖片大小不得大於200kb');</script>";exit();
}
$file = request()->file('upload');//首先這一步不可靠,因為當檔案有錯誤時這邊根本不會有資訊,比如上傳過大,$_FILES有元素,error=1,這邊獲取的元素就為空,所以這部之前應該先處理以下,確保$file不為空
//這一步就需要先檢查下是不是圖片型別了
//$isimage = $file->checkImg();//用的是file的方法
$ext = $file->checkExt('jpg,png,gif,jpeg');//'gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'
$yange = $file->checkImg();
if($ext && $yange){
list($width, $height, $type, $attr) = getimagesize($file->getPathname());
if($width>1000){
//echo "<script type='text/javascript'>window.top.alert('圖片寬度不得大於1000')</script>";exit();
echo "<script>window.parent.CKEDITOR.tools.callFunction($callback, '', '圖片寬度不得大於1000px');</script>";exit();
}
if($height>500){
//echo "<font color=\"red\"size=\"2\">圖片高度不得大於500</font>";exit();
echo "<script>window.parent.CKEDITOR.tools.callFunction($callback, '', '圖片高度不得大於500px');</script>";exit();
}
}else{
echo "<script>window.parent.CKEDITOR.tools.callFunction($callback, '', '請上傳圖片型別');</script>";exit();
}
$info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'ckeditor');
$data = array();
if($info){
$previewname = DS.'public' . DS . 'ckeditor' . DS . $info->getSaveName();
$previewname = str_replace('\\','\/',$previewname);
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($callback,'".$previewname."','');</script>";
}else{
echo "<script>window.parent.CKEDITOR.tools.callFunction($callback, '', '".$file->getError()."');</script>";exit();
}