lara_vel's avatar

Save image path to database (Local Storage)

Hi there! I am able to upload the image to folder. But still on database column, the image path is tmp (like C:/xampp/tmp/php73A8.tmp). What is happening here? Should I use local Storage technique or I am messed up with this code?

if($request->hasFile('productimage'))
       {
           $fileNameExt = $request->file('productimage')->getClientOriginalName();
           $fileName = pathinfo($fileNameExt, PATHINFO_FILENAME);
           $fileExt = $request->file('productimage')->getClientOriginalExtension();
           $fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
           $pathToStore = $request->file('productimage')->storeAs('public/images',$fileNameToStore);
       }
        
       $post = new Post;
       if($request->hasFile('productimage')){
                   $post->productimage = $fileNameToStore;
               }
0 likes
6 replies
kirankrishnan's avatar

@lara_vel

I think you have to use the variable $pathToStore instead of $fileNameToStore to save to the database.

From Laravel documentation "The path to the file will be returned by the store method so you can store the path, including the generated file name, in your database."

jlrdw's avatar

This code is working fine for me in laravel 5.5

    public function add(Request $request)
    {
        if (isset($_POST['submit'])) {
            $lid = DB::table('dc_dogs')->count();
            $lid = $lid + 1;
            $file = $request->file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];
            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ASSET . 'upload/imgdogs';
            echo $destinationPath;
            $file->move($destinationPath, $newname);
            $dogpic = Cln::fixValue($newname);
            $dogname = ucfirst(Cln::fixValue($_POST['dogname']));
            $sex = ucfirst($_POST['sex']);
            $comments = Cln::fixValue($_POST['comments']);
            $adopted = (isset($_POST['adopted']) == '1' ? '1' : '0'); ///added
            $lastedit = date("Y-m-d H:i:s");
            //echo "adpt=" . $adopted . "aaa";

            if (!isset($error)) {
                $postdata = array(
                    'dogpic' => $dogpic,
                    'dogname' => $dogname,
                    'sex' => $sex,
                    'comments' => $comments,
                    'adopted' => $adopted,
                    'lastedit' => $lastedit
                );

                DB::table('dc_dogs')->insert($postdata);
            }
        }
        $title = 'Dog add';
        $view = 'dog/add';
        $layout = ViewLayout::getLayout('admin/addtp');  //ignore custom stuff
        $content = View::make($view);


        return view($layout)->with('content', $content)->with('title', $title);
    }

But remember to have multipart/form-data

<form action='add' method='post' enctype="multipart/form-data">
// more.

If you prefer a static call, which I do then

change

use Illuminate\Http\Request;

to

use Illuminate\Support\Facades\Request;

change

public function add(Request $request)

to

public function add()

change

$file = $request->file('ufile');

to

$file = Request::file('ufile');

And the constant ASSET I defined in index.php

define('ASSET', realpath(dirname(__FILE__)). DS . 'assets' . DS);

DS needs a define also, otherwise use DIRECTORY_SEPARATOR

defined('DS') || define('DS', DIRECTORY_SEPARATOR);
lara_vel's avatar

@kirankrishnan I tried. Sorry, but it also did not help me. Still it references the same path C:/xampp/tmp/php73A8.tmp but if I return the path with

return $pathToFile;

It shows the correct path. But it does not save the correct path on database table.

lara_vel's avatar

@kirankrishnan I have code a registered user to publish a post. My code is

 auth()->user()->save(
            new Post(request(['title', 'body', 'productimage']))
        );
kirankrishnan's avatar
$post = new Post;

if($request->hasFile('productimage')){
    $post->productimage = $fileNameToStore;
}

$post->user_id = Auth::id();

$post->save();

Please or to participate in this conversation.