Evin's avatar
Level 1

Issue with image uploading

Hi all,

I have been working with Laravel for two months now, but ran on to the first issue my google-fu could not answer.

With the following form I make a new article entry:

{{ csrf_field() }}
            <div class="form-group{{ $errors->has('cateogry_id') ? ' has-error' : '' }}">
                <label for="`title" class="col-md-4 control-label">Spel</label>

                <div class="col-md-8">

                    <select class="form-control" id="category_id" name="category_id" required="">
                        @foreach ($categories as $category)
                        <option value="{{ $category->id }}">{{  $category->title }}</option>
                        @endforeach
                    </select>

                    @if ($errors->has('category_id'))
                    <span class="help-block">
                        <strong>{{ $errors->first('cateogry_id') }}</strong>
                    </span>
                    @endif
                </div>
            </div>

            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                <label for="`title" class="col-md-4 control-label">Title</label>

                <div class="col-md-8">
                    <input id="title" type="text" class="form-control" name="title" value="{{ old('title') }}" required autofocus>

                    @if ($errors->has('title'))
                    <span class="help-block">
                        <strong>{{ $errors->first('title') }}</strong>
                    </span>
                    @endif
                </div>
            </div>

            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                <label for="`body" class="col-md-4 control-label">body</label>

                <div class="col-md-8">
                    <textarea name="body"  id="body" type="text" class="form-control" name="body" value="{{ old('body') }}" ></textarea>

                    @if ($errors->has('body'))
                    <span class="help-block">
                        <strong>{{ $errors->first('body') }}</strong>
                    </span>
                    @endif
                </div>
            </div>

            <div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
                <label for="`body" class="col-md-4 control-label">Afbeelding</label>

                <div class="col-md-8">
                    <input type="file" name="image"  id="image" name="pic" accept="image/*" value="{{ old('image') }}>

                    @if ($errors->has('image'))
                    <span class="help-block">
                        <strong>{{ $errors->first('image') }}</strong>
                    </span>
                    @endif
                </div>
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary">
                    Toevoegen
                </button>
            </div>
        </form>

And I use the following function in the controller:

public function store(Request $request){ // Function that stores the new category.

    // Validation.
    // $this->validate($request, [
    //  'user_id' => 'required',
    //  'category_id' => 'required',
    //  'title' => 'required|max:300',
    //  'body' => 'required',
    //  ]);


    $user = Auth::user();
    $article = new Article;

    echo $article->user_id = $user->id;
    echo '<br>';
    echo $article->category_id = $request->category_id;
    echo '<br>';
    echo $article->title = $request->title;
    echo '<br>';
    echo $article->body = $request->body;
    echo '<br>';

    if ($request->hasFile('image')) {
        if ($request->file('image')->isValid()) {

        // getting all of the post data
        $path = 'img'; // upload path
        echo $path;


        echo 'file extension: <br>';
        $extension = null;
        $extension = $request->image->extension(); // getting image extension
        echo $extension;
        echo 'filename: <br>';
        echo $fileName =  $request->file('image')>getClientOriginalExtension(); // get the files original filename
        Request::file('image')->move($path, $fileName); // uploading file to given path

        echo $article->image = $path.$fileName;
        echo '<br>';

    } else {
        echo 'image is not valid';
    }

} else {
    echo 'request has no image';
}

    // $article->save();
    // return back();

}

As I get request has no image, obviously I am doing something wrong.

0 likes
3 replies
ohffs's avatar
ohffs
Best Answer
Level 50

Do you have enctype="multipart/form-data" on your form tag?

Evin's avatar
Level 1

I did not ;)

Now that works and the image is stored in Storage. However, I would like to store it in the public folder in a subfolder called 'img'.

dende's avatar

I simply move the file after uploading...

public static function putFile($file, $folder = "uploads")
{
    $file = Req::file('file');
    $extension = $file->getClientOriginalExtension();
    $fileName = $file->getFilename() . '.' . $extension;
    File::move($file, storage_path($folder) . "/" . $fileName);

    return $fileName;
}

Please or to participate in this conversation.