George Rivera
George Rivera

Reputation: 261

Laravel 4 - File Upload

I have a Form where among other info, a user would upload an image. I want to store the path to the image on the database and save the image to the public/img/ folder on the Server.

The form was opened using: {{ Form::open(['route'=>'pizzas.store', 'files'=>true]) }}so that it would be able to POST files. Inspecting the HTML I have the folowing:

<form method="POST" action="http://mypizza/pizzas" 
  accept-charset="UTF-8" enctype="multipart/form-data">

I can POST to my Controller and receive all the data from the Form as expected. The method handling the file is as follows:

public function store() {
    //TODO -  Validation

    $destinationPath = '';
    $filename        = '';

    if (Input::hasFile('image')) {
        $file            = Input::file('image');
        $destinationPath = '/img/';
        $filename        = str_random(6) . '_' . $file->getClientOriginalName();
        $uploadSuccess   = $file->move($destinationPath, $filename);
    }


    $pizza = Pizza::create(['name'       => Input::get('name'),
                           'price'       => Input::get('price'),
                           'ingredients' => Input::get('ingredients'),
                           'active'      => Input::get('active'),
                           'path'        => $destinationPath . $filename]);

    if ($pizza) {
        return Redirect::route('pizzas.show', $pizza->id);
    }

    //TODO - else
}

When I select a File and Submit the Form, everything seems to work, except that no file is saved on the /img folder. The database registers the file path and name correctly.

Running dd($uploadSuccess);right after the if { ...}block, I get the following:

object(Symfony\Component\HttpFoundation\File\File)#220 (2) {
  ["pathName":"SplFileInfo":private]=> string(17) "/img\YZmLw7_2.jpg"
  ["fileName":"SplFileInfo":private]=> string(12) "YZmLw7_2.jpg" }

What am I doing wrong?

Upvotes: 23

Views: 39006

Answers (5)

Md Shahadat Hossain
Md Shahadat Hossain

Reputation: 347

you can write relative path also. like as

$destinationPath="resources/assets/images/";

or

$destinationPath= 'public/img/';

Upvotes: 1

blakroku
blakroku

Reputation: 551

i had the same problem. I got everything right about my code except for the

public_path()

Wasn't prepending to the my folder

$destinationPath= public_path() . '/img/'; // Worked perfect

I also did this to change my file name

I got the file extension

$extension = Input::file('YourFileName')->getClientOriginalExtension();
$filename = WhateverYouWantHere . '.' . $extension;

And that is it, file name changed. Laravel is indeed wonderful

Upvotes: 1

arpit desai
arpit desai

Reputation: 79

$destinationPath= public_path() . 'img/';

answer given by @Reflic may br right... but not for me....
this path worked for me.
it may because i removed "public/" from url... thank you.

Upvotes: 1

Travis Bennett
Travis Bennett

Reputation: 96

Since you appear to be using php 5.4 you also might consider using Stapler. It's quite stable now (will be out of beta very shortly) and would save you from a lot of the boilerplate that you're having to write right now.

Upvotes: 3

Reflic
Reflic

Reputation: 1421

Your $destination_path is wrong. You have to include the path to the /public directory in your variable $destination like this:

$destinationPath = public_path().'/img/';

Upvotes: 33

Related Questions