Redactor image loading not working on laravel 5
The same code works in laravel 4 but doesn't work in laravel 5.
Here are all the codes:
//Redactor Image Upload
Route::post('image/upload', function(){
$image = Input::file('file');
$filename = 'bdtimes'.rand(10, 99999999).'.'.$image->getClientOriginalExtension();
$move = Image::make($image->getRealPath())->save('uploads/images/original/'.$filename);
if($move){
return Response::json(['filelink'=>'/uploads/images/original/'. $filename]);
}else{
return Response::json(['error'=>true]);
}
});
Redactor Script:
$(function()
{
$('#redactor').redactor({
focus: true,
imageUpload: '{{ url() }}/image/upload',
imageManagerJson: '{{ url() }}/image.php',
plugins: ['table', 'video','imagemanager','fontcolor','fontsize','fullscreen'],
maxHeight: 300,
minHeight: 300
});
});
The Chrome Developer Tool shows this error when I try to upload an image.
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:8000/image/upload
What is the problem? Please help me.
thank
source to share
Updated answer
Token problem. Modify the Redactor script ..
$(function()
{
$('#redactor').redactor({
focus: true,
imageUpload: '{{ url() }}/image/upload?_token=' + '{{ csrf_token() }}',
imageManagerJson: '{{ url() }}/image.php',
plugins: ['table', 'video','imagemanager','fontcolor','fontsize','fullscreen'],
maxHeight: 300,
minHeight: 300
});
});
source to share
This means that some line of your route has an error. Looking at my code, I don't see any obvious problem, it might have to do with imports, etc., which is not shown here.
Try using dd () after each line to print the debug information until you find the exact line that is broken.
You can also look at the response to the ajax request from Chrome Developer, as it should have more information on the exact error.
source to share
I had the same problem while using redactor with yii framework. I think the problem is with the route setting to the download directory. Therefore, the redactor developers changed the code to prevent this. In RedactorModule.php> public function getSaveDir () change this:
$path = Yii::getAlias($this->uploadDir);
if (!file_exists($path)) {
throw new InvalidConfigException('Invalid config $uploadDir');
}
if (FileHelper::createDirectory($path . DIRECTORY_SEPARATOR . $this->getOwnerPath(), 0777)) {
return $path . DIRECTORY_SEPARATOR . $this->getOwnerPath();
:
$path = Yii::getAlias($this->uploadDir) . DIRECTORY_SEPARATOR . $this->getOwnerPath();
if(!file_exists($path)){
if (!FileHelper::createDirectory($path, 0775,$recursive = true )) {
throw new InvalidConfigException('$uploadDir does not exist and default path creation failed');
}
}
return $path;
source to share