Ckeditor upload picture in laravel project - laravel

I have integrated ckeditor in my laravel project, and when i try add a picture in the text after i click on save post. I Get an error in the console. POST https://encode.ba/admin/lista-vijestis/4 403 (Forbidden)
The upload working fine, permission on the storage folder is set properly
and of course, don't record my post
it seems to me that the server is blocking something, I'm not sure, that's why I'm asking, maybe someone has a similar experience
I downloaded the project to my computer and ran it on a local server and everything works fine.
here is a screenshot
enter image description here
enter image description here
Here is first screen shot when i try add post.

In your html page:
<textarea name="description" placeholder="Enter the Description"></textarea>
In script:
<script src="https://cdn.ckeditor.com/4.12.1/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('description', {
filebrowserUploadUrl: "{{ route('admin.product.uploadMedia', ['_token' => csrf_token()]) }}",
filebrowserUploadMethod: 'form'
});
</script>
create Route:
Route::post('product/img', [ProductController::class, 'uploadMedia'])->name('admin.product.uploadMedia');
In Controller:
public function uploadMedia(Request $request)
{
if ($request->hasFile('upload')) {
$originName = $request->file('upload')->getClientOriginalName();
$fileName = pathinfo($originName, PATHINFO_FILENAME);
$extension = $request->file('upload')->getClientOriginalExtension();
$fileName = $fileName . '_' . time() . '.' . $extension;
$path = storage_path('app/public/tmp/uploads');
$fileName = $request->file('upload')->move($path, $fileName)->getFilename();
$CKEditorFuncNum = $request->input('CKEditorFuncNum');
$url = asset('storage/tmp/uploads/' . $fileName);
$msg = 'Image uploaded successfully';
$response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
#header('Content-type: text/html; charset=utf-8');
echo $response;
}
return false;
}

Related

Laravel storage get file causes problem in Google Chrome

I tried to get file from storage of my Laravel Application like following: (Notice: In Firefox Files in pdf / jpeg format are directly been showed. Other files like docx or something is causing download).
$contents = Storage::get("files/" . $file);
return $contents;
In Google Chrome file is not showing and just shows me source of file like this (Following is example if I have a jpeg image) My whole desktop is full of following signs:
ÿØÿà�JFIF������ÿÛ�„�
Do I have to set headers or something to show files in Google Chrome?
You can try instead :
$content = Storage::get("files/" . $file);
if (!File::exists($content)) {
abort(404);
}
$file = Storage::get($content);
$type = Storage::mimeType($content);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
Try this.
$file = File::get("files/" . $file);
$type = File::mimeType("files/" . $file);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);

Laravel 5.8 image failed to upload

I cannot seem to upload an image file with Laravel. I keep getting the photo failed to upload.
My form:
<form id="save_report_form" action="{{ route('report.add') }}" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="upload-photo" id="image" accept="image/png,image/jpg" />
</form>
My Controller:
public function add(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:png,jpg',
]);
// Get all file details and store in public
$disk = Storage::disk('public');
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = $file . '.' . $ext;
$disk->put($filename, file_get_contents($file), 'public');
return redirect()->back();
}
I have changed upload_max_filesize to 20mb for my dev server.
Where can I look to find the reason for the upload failure? I am not getting anything in the Laravel log. What have I missed. Thanks.
can you replace
$filename = $file . '.' . $ext;
with
$filename = $request->image->getClientOriginalName();
You can check what is the result of $filename with dd() /dd($filename) function and see if it is what you expect. If it is not, probably there is the problem.
Also as far as i see in the official docs(https://laravel.com/docs/5.8/filesystem#file-visibility) the usage of put() method is exampled without php native file_get_contents(check this also).

Referencing images using Laravel framework

I'm trying to referencing an image I Laravel but I don't know how to adding
<img src="{{ asset('images/avatar/helmet/'.Auth::user()->avatar()->first()->helmet.'.png')}}">
Did you save the file name with its original extension in the data base? If so, the code below should work.
<img src="{{ asset('images/avatar/helmet/'.Auth::user()->avatar)}}">
I will recommend to save the file with its original extension by adding ->getClientOriginalExtension().
It will look like this:
$image = $request->file('imagename');
if (isset($image))
{
$imagename = uniqid() .'-'. $image->getClientOriginalExtension();
if (!file_exists('Uploads/Media/Slider'))
{
mkdir('Uploads/Media/Slider', 0777 , true);
}
$image->move('Uploads/Media/Slider',$imagename);
}else {
$imagename = 'slider.png';
}

New Version of Chrome cannot render dd function in network preview window Laravel

I want to share live saver with people who debugging with dd() and have to refresh every time because it gets status 200 and stuck.
If you want to change status to 500 for your ajax requests you just need to update
project/vendor/symfony/var-dumper/Dumper/HtmlDumper.php
class on line 111 where dump() method exists.
Just add line http_response_code(500); at the beginning of function. It works for Laravel 5.6 version.
Answer I found in: https://github.com/laravel/framework/issues/21808
See my solution at
https://gist.github.com/fontenele/7625cb71c0a8356213abc727278b48d5
I created a new helper, get the content, replaced div for span because google does not permit <DIV> tags in DevTools.
use Illuminate\Support\Debug\Dumper;
if (!function_exists('_dd')) {
function _dd(...$args)
{
$content = '<span>';
ob_start();
foreach ($args as $x) {
(new Dumper)->dump($x);
}
$content .= ob_get_contents();
ob_end_clean();
$content.= '</span>';
$content = str_replace(['<div', '</div>'], ['<span', '</span>'], $content);
response()->make($content, 500, ['Content-Type' => 'text/html'])->send();
die(1);
}
}
Updated
Lumen:
function _dd(...$args)
{
$content = '<span>';
ob_start();
dump(...$args);
$content .= ob_get_contents();
ob_end_clean();
$content .= '</span>';
$content = str_replace(['<div', '</div>'], ['<span', '</span>'], $content);
response()->make($content, 500, ['Content-Type' => 'text/html'])->send();
die(1);
}

Laravel Retrieve Images from storage to view

I am using below code to store the uploaded file
$file = $request->file($file_attachment);
$rules = [];
$rules[$file_attachment] = 'required|mimes:jpeg|max:500';
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()
->with('uploadErrors', $validator->errors());
}
$userid = session()->get('user')->id;
$destinationPath = config('app.filesDestinationPath') . '/' . $userid . '/';
$uploaded = Storage::put($destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(), file_get_contents($file->getRealPath()));
The uploaded files are stored in storage/app/2/filename.jpg
I want to show back the user the file he uploaded. How can i do that?
$storage = Storage::get('/2/filename.jpg');
I am getting unreadable texts. I can confirm that the file is read. But how to show it as an image to the user.
Hope i made my point clear.
Working Solution
display.blade.php
<img src="{{ URL::asset('storage/photo.jpg') }}" />
web.php
Route::group(['middleware' => ['web']], function () {
Route::get('storage/{filename}', function ($filename) {
$userid = session()->get('user')->id;
return Storage::get($userid . '/' . $filename);
});
});
Thanks to: #Boghani Chirag and #rkj
File not publicly accessible like you said then read file like this
$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg');
Assuming your file is at path storage/app/{$userid}/file.jpg
and default disk is local check config/filesystems.php
File publicly accessible
If you want to make your file publicly accessible then store file inside this storage/app/public folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public then you have to just create a symbolic link and laravel has artisan command for it.
php artisan storage:link
This create a symbolic link of storage/app/public to public/storage. Means now you can access your file like this
$contents = Storage::disk('public')->get('file.jpg');
here the file physical path is at storage/app/public/file.jpg and it access through symbolic link path public/storage/file.jpg
Suppose you have subfolder storage/app/public/uploads where you store your uploaded files then you can access it like this
$contents = Storage::disk('public')->get('uploads/file.jpg');
When you make your upload in public folder then you can access it in view
echo asset('storage/file.jpg'); //without subfolder uploads
echo asset('storage/uploads/file.jpg');
check for details https://laravel.com/docs/5.6/filesystem#configuration
Remember put your folder in storage/app/public/
Create the symbolic linksymbolic link to access this folder
php artisan storage:link
if you want to access profile images of 2 folder then do like this in your blade file
<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />
Laravel 8
Controller
class MediaController extends Controller
{
public function show(Request $request, $filename)
{
$folder_name = 'upload';
$filename = 'example_img.jpeg';
$path = $folder_name.'/'.$filename;
if(!Storage::exists($path)){
abort(404);
}
return Storage::response($path);
}
}
Route
Route::get('media/{filename}', [\App\Http\Controllers\MediaController::class, 'show']);
Can you please try this code
routes.php
Route::group(['middleware' => ['web']], function() {
Route::get('storage/storage_inner_folder_fullpath/{filename}', function ($filename) {
return Image::make(storage_path() . '/storage_inner_folder_fullpath/' . $filename)->response();
});
});
view file code
<img src="{{ URL::asset('storage/storage_inner_folder_fullpath/'.$filename) }}" />
Thanks
Try this:
Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');
Good Luck !
Uploaded like this
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
"public/avatar",
$uploadedFile,
$photo
);
and then access like this
<img src="{{ asset('storage/avatar/'.$filename) }}" />
Create Route:
Route::get('image/{filename}', 'HomeController#displayImage')->name('image.displayImage');
Create Controller Method:
public function displayImage($filename)
{
$path = storage_public('images/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">
can You please try this
$storage = Storage::get(['type_column_name']);

Resources