All,
Case: Uploading profile picture. This is working offline on my localhost, but after putting it online to my hosting provider, this does not work anymore. The file is NOT being uploaded but Laravel does not returns any error ...
The destination folder exists on the online server and has permission 777. And the fila is a jpg of 729KB. Host has uploading enabled.
My code:
Controller:
public function updateProfile(UpdateUserRequest $request)
{
//
$pass = false;
$this->validate($request, [
'password' => 'confirmed',
'profile_pic' => 'mimes:jpeg,jpg,bmp,png,gif,svg',
]);
$user = User::findOrFail($request->get('id'));
$user->username = $request->get('username');
$user->name = $request->get('name');
$user->email = $request->get('email');
if (!empty($request->get('password'))) {
$user->password = bcrypt($request->get('password'));
$pass = true;
}
if ($request->file('profile_pic')->isValid()) {
$destinationPath = public_path().'/images/users'; // upload path
$extension = $request->file('profile_pic')->getClientOriginalExtension(); // getting image extension
$fileName = uniqid() . '.' . $extension; // renameing image
$request->file('profile_pic')->move($destinationPath, $fileName); // uploading file to given path
$user->profilepic = $fileName;
}
$user->save();
if ($pass === false) {
return redirect('user/profile')->with('message', 'Profile saved!');
} else {
return redirect('user/profile')->with('message', 'Profile and new password saved!');
}
}
Form:
{!! Form::open(array('method' => 'PATCH', 'action' => 'UserController@updateProfile', 'class' => 'form', 'files' => true)) !!}
{!! Form::file('profile_pic', ['class' => 'form-control']) !!}
Dump and Die on online host:
UploadedFile {#29 ▼
-test: false
-originalName: "placeholder.jpg"
-mimeType: "image/jpeg"
-size: 810926
-error: 0
}
Anyone has an idea why this is not working online while it's works offline?
Thanks in advance, 3s
Fixed! The problem was that on the online server, the application folder is placed OUTSIDE the public_html folder. Therefore the public_path() was pointing at the "wrong" folder.
# root
## laravel
## public_html
### images
#### users
Therefore the application was uploading the images into
# root
## laravel
### public
#### images
I managed to resolve this with a quick fix in the controller.
if ($request->file('profile_pic')->isValid()) {
$path = base_path();
$path = str_replace("laravel", "public_html", $path); // <= This one !
$destinationPath = $path . '/images/users'; // upload path
$extension = $request->file('profile_pic')->getClientOriginalExtension(); // getting image extension
$fileName = uniqid() . '.' . $extension; // renameing image
$request->file('profile_pic')->move($destinationPath, $fileName); // uploading file to given path
$user->profilepic = $fileName;
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community