I'm uploading a file in laravel. The path is storing in database but unable to store image into destination folder.
Here is my controller.

public function post_contact()
{
$contact = new Contact;
                $contact->name   =       Input::get('name');
                $contact->email   =       Input::get('email');
                $contact->mobile   =       Input::get('mobile');
                $contact->password   =       Input::get('password');
                $contact->confirm   =       Input::get('confirm');
                if($contact->save())
                {
                    $data = Input::except('_token');
                    $contact->upload    =       $data['upload'];
                    $time=time();
                    if(Input::hasFile('upload'))
                    {
                        $file = Input::file('upload');
                        $image=$time . '.' .$file->getClientOriginalName();
                        $file->move_uploaded_file(public_path().'../app/uploads', $image);
                    }  
                    $contact->save();
                }
                return Redirect::route('contact')->with('success','Successfully Saved');
            }   

Recommended Answers

All 4 Replies

Member Avatar for diafol

Here's a user defined library method I use:

public static function storeFile($fieldName, $uploadFolder)
{
    if (Input::hasFile($fieldName))
    {
        if (Input::file($fieldName)->isValid())
        {
             if($uploadFolder == 'customers')
             {
                $prefix = 'cf';
             }elseif($uploadFolder == 'loans'){
                $prefix = 'ln';
             }else{
                 return false;
             }

             $extension = Input::file($fieldName)->getClientOriginalExtension();
             $filename = $prefix . "_"  . mt_rand(1000000000,9999999999) . "." . $extension;

             $destinationPath = public_path() . "/fileuploads/$uploadFolder/";

             if(Input::file($fieldName)->move($destinationPath, $filename))
             {
                return $filename;
             }
        }
    }
    return false;
}

Usage:

$fieldName = 'fileupload';
$uploadFolder = 'loans';

if( $filename = General::storeFile($fieldName, $uploadFolder) )
{
    //store filename to DB
}else{
    //raise error
}

So it may be this line:

 $file->move_uploaded_file(public_path().'../app/uploads', $image);

Try:

 $file->move(public_path().'../app/uploads', $image);

I work out on this code and now the databse storing the wrong path of the image but the image is get stored in desired location.
Here is the code.

public function post_contact()
{
                $contact = new Contact;
                $contact->name   =       Input::get('name');
                $contact->email   =       Input::get('email');
                $contact->mobile   =       Input::get('mobile');
                $contact->password   =       Input::get('password');
                $contact->confirm   =       Input::get('confirm');
                if($contact->save())
                {
                    $contact->upload    =       $data['upload'];
                    $time=time();
                    if(Input::hasFile('upload'))
                    {
                        $file = Input::file('upload');
                        $img=$time . '.' .$file->getClientOriginalName();
                        $file->move('../app/uploads/', $img);
                    }  
                    $contact->save();
                }
                return Redirect::route('contact')->with('success','Successfully Saved');
            }

Databse store the path like this
D:\xampp\tmp\php9EB7.tmp

Member Avatar for diafol

Where are you storing the path in your code? I'd imagine:

$contact->file = $img;

somewhere

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.