Just add the user ID as a folder in between?
$user = User::auth();
$user_folder = url('images/'. $user->id);
Sorry about the bad wording of the title, i've followed a tutorial on uploading images from a profile to a public uploads folder, i was wondering if there's a simple / efficient way to upload files / images to a set users folder to keep the files organised as such? Instead of everything being dumped into a uploads folder.
Kinda new here so let me know if this isn't relevant!
~Owen
Just add the user ID as a folder in between?
$user = User::auth();
$user_folder = url('images/'. $user->id);
Hey @skoTner thanks for the reply, maybe it might help if i place some code.
I'm using this line to create the file and store in the public path.
Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/profile_pictures/' . $filename ) );
edit: the upload needs to create the directory, enter it and save the file.
Don't know how you set your $avatar variable, but I presume it's from a file upload. So I would have done (and cleaned it up a bit):
$user = User::auth();
$avatar = Input::file('avatar');
$path = public_path('uploads/profile_pictures/'. $user->id .'/'. $filename);
Image::make($avatar->getRealPath())->resize(300, 300)->save($path);
... I don't know how you set your filename, but I would make sure the filename is dynamic as well, for caching purposes.
top man @skoTner - however i need to create the directory with Storage,
this is current code:
public function update_avatar(Request $request) {
//handle uploads
$user = Auth::user();
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$path = public_path('uploads/profile_pictures/'. $user->username .'/'. $filename );
// i need to create this directory and then save the image to it
Storage::MakeDirectory(public_path('uploads/profile_pictures/'.$path));
Image::make($avatar->getRealPath())->resize(300, 300)->save($path);
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
remember to add this line , to avoid permission problem
if (!file_exists($path)) {
mkdir($path,0777,true);
}
I think below code use for save image with resize in public/uploads directory
public function upload_image(Request $request){
$user = User::auth();
$file = $request->file('image');
$destinationPath = public_path('uploads/'.$user->name);
$fileName = $request->image->getClientOriginalName();
$imageResize = Image::make($file->getRealPath())
->resize(150,150,function($c){$c->aspectRatio(); $c->upsize();})->save($destinationPath.'/'.$fileName);
}
Hope this work for you !
You have $path redundant there, so make sure to clean that up. Also make sure to set the permission on your folder as you create it.
In your return view function, you have already instantiated the user, so you don't have to redo it. And it can look a bit cleaner using compact(). See my example below:
public function update_avatar(Request $request) {
// Handle uploads
$user = Auth::user();
if($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$path = public_path('uploads/profile_pictures/'. $user->username .'/'. $filename );
Storage::makeDirectory($path, 0777, true);
Image::make($avatar->getRealPath())->resize(300, 300)->save($path);
$user->avatar = $filename;
$user->save();
}
return view('profile', compact('user'));
}
Does this help?
What about this way ? It's make your image directory clean and structure.
$time = Carbon::now();
$path = $user->name.'/'.date_format($time, 'Y').'/'.date_format($time, 'm');
(Magento upload images)
@skoTner its looking promising mate, i've tried it that way as you've suggested but it's throwing a NotWritableException in Image.php line 143: Can't write image data to path
Please or to participate in this conversation.