Upload file in Laravel - wrong path - php

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);

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?

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');
}

Laravel can't upload file in storage folder

I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.

Laravel 5.4 Upload images with their own names

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

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