bottelet's avatar

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?

0 likes
9 replies
boynet's avatar

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

bottelet's avatar

Yeah i know this, but i want it to be the date and time of now, also thats not the biggest concern right now, its more that its putting everything into the tmp folder. But thanks for the input :)

wing5wong's avatar

You need to set your image_path no?

if(Input::hasFile('image_path')) {
             $file =  Input::file('image_path');

            $destinationPath = public_path(). '/images/';
            $filename = $file->getClientOriginalName();

            $file->move($destinationPath, $filename);
            
        }
$input = $userRequest->all();

image_path from input is going to be the temp folder, not the moved file path.

how about something like this

if(Input::hasFile('image_path')) {
             $file =  Input::file('image_path');

            $destinationPath = public_path(). '/images/';
            $filename = $file->getClientOriginalName();

            $file->move($destinationPath, $filename);
        }
        $input =  array_replace($userRequest->all(), ['image_path'=>$destinationPath]);
       $user = User::create($input);
alrocha's avatar

what you can do is something like that, it creates a folder for each picture that you are uploading with the username

$filename = $photo->getClientOriginalName();
    $photo = Image::make($photo);
    $photo->resize(800, 600);
    $route_path = public_path().'/images/'.Auth::user()->username.'/';
    File::exists($route_path) or File::makeDirectory($route_path);
    $photo->save($route_path.['name'].'-'.$filename);

otherwise you can always use http://www.gravatar.com :)

bottelet's avatar

@wing5wong That seem to almost work, i get the file path to the folder, but not the file name, am i able to do something like,

$input =  array_replace($userRequest->all(), ['image_path'=>$destinationPath, $filename]); 

or

$input =  array_replace($userRequest->all(), ['image_path'=>$file]);

I dont know? Also would you mind explaning why this works? :)

@alrocha That seems like a good idea thanks for the input!

wing5wong's avatar
$input =  array_replace($userRequest->all(), ['image_path'=>"$destinationPath$filename"]); 

should work for you then, you may need to add a dir seperator though, sorry i dont have code on hand to see it

bottelet's avatar

@wing5wong That works, but i just realised it dosen't actully move the pictures to the image folder, any suggestions?

wing5wong's avatar
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 ) ) {
bottelet's avatar

So i got it too work, thanks you @wing5wong!

  • If anyone else finds this what i did was change my $destinationPath = public_path(). '\images\'; to using '\filepath\; instead of '/filepath/; That made it work like the example shown above.

Please or to participate in this conversation.