Laravel4: upload a photo

FrancescoMussi picture FrancescoMussi · Aug 21, 2013 · Viewed 7.1k times · Source

I'm stuck.. I want to add to give the possibity to add a new photo through a form. I have the Photo resource properly set up. The table photo with: id, title, caption, path:string.

This is the form that i made until now in the view photo.create:

{{ Form::open(array('route' => 'photos.store'),'image/save', array('files'=> true)) }}

 {{ Form::label('title', 'Title: ') }}
 {{ Form::text('title') }}

 {{ Form::label('caption', 'Caption: ') }}
 {{ Form::textarea('caption') }} <br>

 {{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}

 {{ Form::close() }}

How can i add a button thanks to which select a new file?

Thank you!!

EDIT:

This is the simple store method i made until now:

public function store()
{
    $input = Input::all();

    $rules = array('title' => 'required', 'path' => 'required');

    $validation = Validator::make($input, $rules);

    if ($validation->passes()) 
    {

        $photo = new Photo();
        $photo->title = $input['title'];
        $photo->caption = $input['caption'];
        $photo->path = $input['path'];

        $photo->save();

        return Redirect::route('photos.index');
    }

    return Redirect::back()->withInput()->withErrors($validation)->with('message','There were validation message');

}

How can i put correctly there that implementation to retrieve the file and store in the folder located in public/img ? And then how can i save that path and put into $photo->path ?

Thank you very much!!

Answer

netvision73 picture netvision73 · Aug 21, 2013

Use Form::file('image')

Then, you can retrieve your uploaded file with Input::file('image') and move it to a destination with Input::file('file')->move(YOUR_DESTINATION_PATH);

References : http://laravel.com/docs/requests#files and http://laravel.com/docs/html#file-input


edit :

To store your uploaded file to public/img : Input::file('file')->move(base_path() . '/public/img');

Storing path in database :

$photo->path = base_path() . '/public/img' . Input::file('file')->getClientOriginalName(); // if you want your real path on harddrive

or

$photo->path = URL::to('img/' . Input::file('file')->getClientOriginalName()); // if you want an exploitable path for http render

second edit See my correction on your form http://paste.laravel.com/KX9

@extends('master')
@section('blog')
<div class="span12 well">
    {{ link_to_route('photos.index', 'Back to index') }}
</div>

<div class="span12 well">
   {{ Form::open(array('route' => 'photos.store', 'files'=> true)) }}
     {{ Form::label('title', 'Title: ') }}
     {{ Form::text('title') }}
     {{ Form::label('caption', 'Caption: ') }}
     {{ Form::textarea('caption') }} 
     {{ Form::label('image', 'Image: ') }}
     {{ Form::file('image') }}
     <br>
     {{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
   {{ Form::close() }}
   <br><br>
   @if($errors->any())
      {{ implode('', $errors->all('<li class="error">:message</li>')) }}
   @endif
</div>
@stop