you see this line "$filename = $file->getClientOriginalName();" here you set the file name to the original file name, if you want to add random string than add it
Jan 11, 2016
9
Level 7
Image upload to user
Hello Laravel I'm trying to attach an image to the users. But have a bit of trouble getting it too work. I've googled and seen other people have the same problem but still can't get it work, the problem is the path it inserts to the DV uses the string "C:\xampp\tmp\PHPsomeRandomNumber.tmp". Instead of the Orginal name + current timestamp or something like that.
This is my Users controller:
public function store(Request $userRequest)
{
$this->validate($userRequest, [
'name' => 'required',
'email' => 'required',
'address' => 'required',
'work_number' => '',
'personal_number' => '',
'password' => 'required|confirmed',
'password_confirmation' => 'required',
'image_path' => ''
]);
if(Input::hasFile('image_path')) {
$file = Input::file('image_path');
$destinationPath = public_path(). '/images/';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
}
$input = $userRequest->all();
User::create($input);
//dd($input);
Session::flash('flash_message', 'User successfully added!'); //Snippet in Master.blade.php
return redirect()->back();
}
And this is the form
{!! Form::open([
'route' => 'users.store',
'files'=>true,
'enctype' => 'multipart/form-data'
]) !!}
<div class="form-group">
{!! Form::label('image_path', 'Choose an image:', ['class' => 'control-label']) !!}
{!! Form::file('image_path', null, ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Create New User', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Why does this happen, i read it's because it get the temp location, and adds it to the DB? But how would i fix it?
Level 17
sorry i cant say, perhaps theres some errors in the log you can see
i found an early project of mine, perhaps you need to remove the trailing slash from the destinationPath
$uploadDir = '\\uploads\\teams\\' . $team->id;
$uploadPath = public_path() . $uploadDir;
$originalFileName = $photo->getClientOriginalName();
$originalFileExtension = $photo->getClientOriginalExtension();
$uploadName = str_random(20) . "." . $originalFileExtension;
if ( $photo->move( $uploadPath, $uploadName ) ) {
Please or to participate in this conversation.