patternphplaravelMinor
Profile picture upload method in Laravel 5.2
Viewed 0 times
picturemethoduploadprofilelaravel
Problem
I am a Laravel newbie so please forgive me for my code... I have a method, in a Laravel controller, that updates the current logged user profile (
Logged user can upload a profile picture, and I manipulate the uploaded file (i.e. remove spaces, create unique name, etc.); since I'd like to reuse the upload routine, I put it on a separate method (
The code is this:
It works fine, but I feel there is a better way of doing this
My questions are:
postProfileGeneral()).Logged user can upload a profile picture, and I manipulate the uploaded file (i.e. remove spaces, create unique name, etc.); since I'd like to reuse the upload routine, I put it on a separate method (
_upload())The code is this:
// get a unique file name
protected function _getUniqueFilename($path, $filename, $extension)
{
$i = 2;
$output = $filename . '.' . $extension;
while(File::exists($path . '/' . $output)){
$output = $filename . '_' . $i . '.' . $extension;
$i++;
}
return $output;
}
// upload method, to reuse the routine
protected function _upload(Request $request, $field, $path = '')
{
$path = ($path != '') ? $path : public_path();
if ($request->hasFile($field)){
$file = $request->file($field);
if ($file->isValid()){
$basename = $file->getClientOriginalName(); // get the original filename + extension
$extension = $file->getClientOriginalExtension(); // get the original extension without the dot
$filename = basename($basename, '.' . $extension); // get the original filename only
$slug = str_slug($filename, '-'); // slug the original filename
$name = $this->_getUniqueFilename($path, $slug, $extension);
$file->move($path, $name);
$output = $path . $name;
}
} else {
$output = $request->input('cur_picture');
}
return $output;
}
// update profile
public function postProfileGeneral(Request $request)
{
$picture = $this->_upload($request, 'picture', config('custom.users_path'));
dd($picture);
// rest of method here, not relevant
}It works fine, but I feel there is a better way of doing this
My questions are:
- since the
getUniqueFilename()and_upload()method can be reused in other controllers too (
Solution
I think your program is good. I repeat, IMO, it's good.
As you said in questions, the answers are
As you said in questions, the answers are
- Move your image manipulation related methods to a TRAIT. that'll help you while using the same code in different controllers. Because, if you want it to be done like this way, you must have to declare objects, if you want to use the same code from different controllers/classes. On the other hand, if you use TRAIT, you just have to use them.
- As you're passing the Request to your method, you may pass the array instead. Otherwise, you may use check the file existence, on the previous method then pass the values to next method. You may wish to use varargs as a method parameter.
Context
StackExchange Code Review Q#123887, answer score: 2
Revisions (0)
No revisions yet.