Laravel 5.4 Upload images with their own names - php

i want to upload my images with their own names. But when i tried, they upload with diffrent names. for example; php0K0Saj.57352.JPG (!?)
my controller;
public function post_Savenews(Request $request)
{
$request->all();
/* out of question
$head = $request->input('head');
$content = $request->input('content');
$keywords = $request->input('keywords'); */
$featured=$request->post_featured;
$extension=$request->post_featured->getClientOriginalExtension();
$photoName = $featured . '.' . rand(11111, 99999) . '.' . $extension;
$request->post_featured->move(public_path('uploads'), $photoName);
News::create(array('head' => $head, 'content' => $content, 'keywords' => $keywords,'post_featured'=>$photoName));
return redirect()->route('index');
}

Update the line.
$photoName = $featured . '.' . rand(11111, 99999) . '.' . $extension;
With
$photoName = $request->post_featured->getClientOriginalName();
Hope this helps

Related

Laravel 8 - Upload storage path changed, how to show old files?

In laravel 8, I updated the storage path for image uploads. It was app/violations/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/', $filename);
$violation->violationStatement = $filename;
}
And it was updated to app/violations/speeding/Toyota/XXX-1234/11-June-2022/11-26 AM/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = 'violation-statement' . '-' . $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/' .
$violation->violationType . '/' .
$violation->carModel . '/' .
$violation->plateNumber . '/' .
Carbon::parse($violation->violationDateTime)->format('d-M-Y/H-i A'), $filename);
$violation->violationStatement = $filename;
}
The images are displayed in datatables, so obviously I'll have to change the image src to the new URL. When I do so, how can I also display the old images of the old directory?

Upload file in Laravel - wrong path

I am beginner in Laravel.
I have this code:
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = 'upload/images/UserImage/';
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
This function uploads files to public/upload/images/UserImage/.
I need it to store it in storage/app/upload/images/UserImage/ instead
How can I rewrite my code?
You have to use storage_path function ("storage/app/upload" folder must exist):
$image->move(storage_path("app/upload"), $uniqueName . '.' . $extension);
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = storage_path('app/public/upload/images/UserImage');
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
A common way in Laravel to upload to storage/app is through the local disk driver.
$file = $path . $uniqueName . '.' . $extension;
\Storage::disk('local')->put($file, $request->file('profilePhoto'));
Storage::disk('local') points to storage/app/.
As your $path variable is already declared like this $path = 'upload/images/UserImage/' .So instead of storing data to public_path you can store to storage_path().
$image->move(storage_path($path), $uniqueName . '.' . $extension);

Image uploaded in Laravel 5.8 keep saving as private image

I am trying upload a single image into a Post and it keeps saving it as a private image--heck, I don't even know where to find that file.
PostController.php
...
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('public/posts/' . $request->user()->id, $filename);
}
...
storeAs() function takes three parameters.
storeAs(path,name,options)
'options' is where you specify file visibility. In your case :
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('posts/' . $request->user()->id, $filename,'public');
}

Image Upload duplication

I am trying to allow users to upload three images(optional) when they make a review of the product. I am encountering a problem with the code below, which is that the images duplicate when the form is submitted which means that i sometimes get two of the same images or all three the same. Does anyone know the solution to this issue or a better way to do three image optional image uploads?
Html:
{{Form::label('image', 'Image(optional')}}
{{Form::file('image')}}
{{Form::label('image2', 'Image(optional')}}
{{Form::file('image2')}}
{{Form::label('image3', 'Image(optional')}}
{{Form::file('image3')}}
Laravel PHP:
$picture = new Picture();
if($request->hasFile('image')){
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->save($location);
$picture->image = $filename;
}
$picture->products()->associate($product);
$picture->user_id = $request->user()->id;
$picture->reviews()->associate($review);
$picture->save();
$picture2 = new Picture();
if($request->hasFile('image2')){
$image = $request->file('image2');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->save($location);
$picture2->image = $filename;
}
$picture2->products()->associate($product);
$picture2->user_id = $request->user()->id;
$picture2->reviews()->associate($review;
$picture2->save();
$picture3 = new Picture();
if($request->hasFile('image3')){
$image = $request->file('image3');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->save($location);
$picture3->image = $filename;
}
$picture3->products()->associate($product);
$picture3->user_id = $request->user()->id;
$picture3->reviews()->associate($review);
$picture3->save();

move 'tmp' directory not working when uploading image in laravel 5.4

When uploading my image it is saved to D:\xampp\tmp\phpD0E0.tmp directory. But I want to save it in public/uploads/banner. Anyone please help me. Here is my code:
BannersController.php
public function store(Request $request)
{
$requestData = $request->all();
if ($request->hasFile('banner_image')) {
foreach($request['banner_image'] as $file){
$uploadPath = public_path('/uploads/banner');
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
$file->move($uploadPath, $fileName);
$requestData['banner_image'] = $fileName;
}
}
Banner::create($requestData);
Session::flash('flash_message', 'Banner added!');
return redirect('Banner/banners');
}
Can you please revert that path to 'root' => storage_path('app/public')
and just try with change line:
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
with this:
$fileName = rand(11111, 99999) . '.' . $file->getClientOriginalName();
Hope this helps you.

Categories