Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
George Rivera Avatar asked Sep 27 '13 21:09

George Rivera


People also ask

How can I upload my file name in Laravel?

The hashName method is exactly what Laravel calls in the store method. $request->image->hashName(); You will get the same name that Laravel generates when it creates the file name during the store method. $path = $request->image->getClientOriginalName();


3 Answers

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/';

like image 106
Reflic Avatar answered Nov 01 '22 05:11

Reflic


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.

like image 3
Travis Bennett Avatar answered Nov 01 '22 05:11

Travis Bennett


$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.

like image 1
arpit desai Avatar answered Nov 01 '22 06:11

arpit desai