mozew's avatar
Level 6

How to store upload an image?

I want to store a product and upload an image?

I don,t want to rename it in database

For example:

I have an image, the image name is 1.jpg

When I upload an image 1.jpg, but It save to database rename it

Upload

ProductController

public function store(ProductRequest $request)
{
    if ($request->hasFile('images')) {
        $fileNameWithExt = $request->file('images')->getClientOriginalName();
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        $extention = $request->file('images')->getClientOriginalExtension();
        $fileNameToStore = $filename.'_'.time().'.'.$extention;
        $path = $request->file('images')->storeAs('public/images', $fileNameToStore);
    }
    $product = auth()->user()->products()->create(array_merge($request->all(), ['images' => $path]));
    $product->categories()->attach($request->category);
    return redirect()->route('products.index');
}
0 likes
11 replies
ChristophHarms's avatar

With the code you posted, there is no explanation for the "V" in your uploaded filename. The image should be called 1_[timestamp].jpg.

Either the file in the request is not called "1.jpg" but "V1.jpg", there is something happening here that you did not show us, or getClientOriginalName() does something unexpected.

ChristophHarms's avatar

You got me wrong. I don't have any code. I said that in your code, there is no hint as to why the filename gets stored the wrong way.

Snapey's avatar

There you go again.....

Your code looks like 6 different tutorials crashed into each other.

Look at each line. What does it do? Dump the output at each step so you can understand what it is doing and why you are doing it.

1 like
jlrdw's avatar

I don't know about your code, no idea, but the following works 100%

<form action='add' method='post' enctype="multipart/form-data">
        <table style="border:none; width: 700px;">
            <tr>
                <td>dogpic:</td>
                <td>
                    <input name="ufile" type="file" id="ufile" size="50" /></td>
                </td>
            </tr>
             ///////////// more code for form

controller

           $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'; // use your path here not mine.
            $file->move($destinationPath, $newname);

            $dogpic = $newname;
           
            $dogname = ucfirst(Request::input('dogname'));
            $sex = ucfirst(Request::input('sex'));
            $comments = Request::input('comments');
            $adopted = !empty(Request::input('adopted')) ? '1' : '0';
            $lastedit = date("Y-m-d H:i:s");

            $postdata = array(
                'dogpic' => $dogpic,
                'dogname' => $dogname,
                'sex' => $sex,
                'comments' => $comments,
                'adopted' => $adopted,
                'lastedit' => $lastedit
            );

            DB::table('dc_dogs')->insert($postdata);
           

Never had problems with this.

Validation not shown, so validate your stuff as needed.

And

Answer my question

Sounds very rude, These folks are trying to help, and do so for free, and they enjoy trying to help, this is not PAID support.

Even if one did not help you, maybe they were at least trying. Don't like an answer, skip it then.

Please be nice to folks trying to help, and thank them sometimes.

siangboon's avatar

seem like he just want people to help him code but never try and understanding the code....

mozew's avatar
Level 6

@JLRDW - I change and I tried this code but my problem did not slocve

Look at this code.

public function store(ProductRequest $request)
{
    $product = new Product();
    $product->user_id = auth()->user()->id;
    $product->title = $request->title;
    $product->body = $request->body;
    $product->price = $request->price;

    if($request->has('image')) {
        $file = $request->image;
        $file_name = $file->getClientOriginalName();
        $file_ext = $file->getClientOriginalExtension();
        $fileInfo = pathinfo($file_name);
        $filename = $fileInfo['image'];
        $newname = $filename  . "." . $file_ext;
        $destinationPath = public_path('images/products');
        $file->move($destinationPath, $newname);
    }

    $product->save();
    $product->categories()->attach($request->category);
    return redirect()->route('products.index');
}

I get this error

ErrorException (E_NOTICE) Undefined index: image

jlrdw's avatar

I have no idea why your image is not uploading.

Until you figure it out only upload an image nothing else in the request.

Get that problem resolved first.

Echo out that path make sure it's correct check your folder permissions, etc.

Please or to participate in this conversation.