johnef_sh's avatar

Upload image with has file

I am trying to upload image if the user chose one here is the controller

public function store()
    {
        $project = new Projects();

        if (Input::hasFile('pImage')) {
            $file = Input::file('pImage');
            $destination_path = 'images/projects/';
            $filename = str_random(6) . '_' . $file->getClientOriginalName();
            $file->move($destination_path, $filename);
            $project->pImage = $filename;
        }
        
        $project->pro_title = Input::get('pName');
        $project->pro_map = Input::get('pMap');
        $project->pro_description = Input::get('pDetails');
        $project->pro_serves = implode(",", array_filter(Input::get('pro_serves')));
        $project->pro_activity = Input::get('activity');
        $project->save();

        return Redirect::to('admin/view-project')->with('message', 'Project add successfully');
    }

the page returns without the image what I am doing wrong here Help a Laravel beginner :)

0 likes
3 replies
spekkionu's avatar

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

SaeedPrez's avatar

If you're using Laravel 5.3, this is how you upload files..

if (request()->hasFile('pImage')) {
    $project->pImage = request()->file('pImage')->store('images/projects');
}

Also don't forget that your <form> needs attribute enctype="multipart/form-data"

johnef_sh's avatar

Hay guys yes I do have multipart/form-data @SaeedPrez I am using Laravel 4.2 here is my Form header

{{ Form::open(array('url'=>'doAddProject', 'file'=>'true', 'method'=>'PUT')) }}

Please or to participate in this conversation.