1
votes

I am trying to take a file that is uploaded by the user and move it to a folder named "Images" inside of my Laravel Public file, I have managed to get it to execute successfully with no errors, my only issue is that the uploaded file wont show inside of the /Images folder.

My function is as follows:

public function contactPost(Request $request)
 {

    if($request->hasFile('attachment'))
    {
        $file = $request->file('attachment');
        $file->move('Users/jvb/laravel/public/Images', $file->getClientOriginalName());
        echo 'File name '.$file->getClientOriginalName();
        echo "<br>";
    } else {
        return "No";
    }
 }

The blade.php file is as follows:

@extends('layouts.master')


 @section('content')
<form method="POST" enctype="multipart/form-data" action="{{ route('contactPost') }}">

    @csrf

    <div class="form-group">
        <input class="form-control" name='name' type="text" placeholder="Name">    
    </div>

    <div class="form-group">
        <input class="form-control" name='email' type="email" placeholder="Email">    
    </div>

    <div class="form-group">
        <textarea class="form-control" name="message" id="" cols="30" rows="10" placeholder="Message"></textarea>
    </div>

     <div class="form-group">
        <input class="form-control" name="attachment" type="file">    
    </div>

    <div class="form-group">
        <button type="submit" class="form-control">Send Message</button>
    </div>

</form>
 @endsection

Location of image folder within my laravel project is:

location of Image folder

What am I doing wrong and how fix this?

1
did you go to that directory and see if files are in it? (not using your IDE/editor)lagbox
Yep I used my terminal, nothing shows.Lo-urc

1 Answers

4
votes

Use public_path() instead of absolute path

 $file->move(public_path()."/Images/", $file->getClientOriginalName());

UPDATE In order to store the image into your database

 $user = new User; //model
  $filename = $file->getClientOriginalName();
  $file->move(public_path("Images"), $filename);
  $path = '/Images/' . $filename;
  $user->avatar = $path;
  $user->save();