Php Laravel在CKEDITOR上上传图像
当我使用fromPhp Laravel在CKEDITOR上上传图像,php,laravel,laravel-5,ckeditor,Php,Laravel,Laravel 5,Ckeditor,当我使用fromckeditor上传图像并将其附加到控制器中的post上时,我的上传图像功能工作正常,没有任何问题,但当我想将上传的图像返回到该位置时,ckeditor无法获取该图像,例如,这是我的代码: 控制器: public function uploadImageContent() { $this->validate(request(), [ 'upload' => 'mimes:jpeg,jpg,gif,png' ]); $file
ckeditor
上传图像并将其附加到控制器中的post上时,我的上传图像功能工作正常,没有任何问题,但当我想将上传的图像返回到该位置时,ckeditor无法获取该图像,例如,这是我的代码:
控制器:
public function uploadImageContent()
{
$this->validate(request(), [
'upload' => 'mimes:jpeg,jpg,gif,png'
]);
$file = request()->file('upload');
$filename = $file->getClientOriginalName();
$year = Carbon::now()->year;
$imagePath = "/uploads/post_images/{$year}/";
if (file_exists(public_path($imagePath) . $filename)) {
$filename = Carbon::now()->timestamp . '.' . $filename;
}
$file->move(public_path() . $imagePath, $filename);
$url = $imagePath . $filename;
return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
}
class ContentsController extends Controller
{
...
public function attachImage()
{
$this->uploadImageContent(request()->all());
}
}
应该是返回路径,但不工作
视图:
ContentsController:
public function uploadImageContent()
{
$this->validate(request(), [
'upload' => 'mimes:jpeg,jpg,gif,png'
]);
$file = request()->file('upload');
$filename = $file->getClientOriginalName();
$year = Carbon::now()->year;
$imagePath = "/uploads/post_images/{$year}/";
if (file_exists(public_path($imagePath) . $filename)) {
$filename = Carbon::now()->timestamp . '.' . $filename;
}
$file->move(public_path() . $imagePath, $filename);
$url = $imagePath . $filename;
return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
}
class ContentsController extends Controller
{
...
public function attachImage()
{
$this->uploadImageContent(request()->all());
}
}
使用
echo
而不是return
解决我的问题:
echo "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
echo“window.parent.CKEDITOR.tools.callFunction(1,{$url},''”;
我在laravel 5.5上有这个问题,你的代码对我不起作用。我观察到的是,您没有将
CKEditorFuncNum
(服务器作为POST变量接收)嵌入到1
的位置,作为callFunction()
的第一个参数。我用$request->CKEditorFuncNum
替换了1
,然后用return
语句代替echo
,一切正常
这是您的代码:echo“window.parent.CKEDITOR.tools.callFunction(1,{$url},''”代码>
及
这是我的代码:
return“window.parent.CKEDITOR.tools.callFunction('{$request->CKEditorFuncNum}','{$url}',''”代码>
我在Laravel 5.8上
希望它能帮助其他人。这是唯一包含script
html标记的字符串。Javascript不知道应该将其附加到DOM中,除非您告诉它。@Chay22与此实现类似,在我的其他项目中可以正常工作。您应该看看,您还应该在“attachImage”方法中使用“return”语句,因为在“uploadImageContent”方法中,只将结果返回回调函数,而不返回客户端。
echo "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";