onurzdgn's avatar

upload image

I am using laravel 10. I am trying upload image. This is my form:

<form method="POST" action="{{ url('addProduct') }}">
                    {{ csrf_field() }}
                    <div class="mb-3">
                        <label for="productName" class="col-form-label">Ürün Adı:</label>
                        <input type="text" class="form-control" id="productName" name="productName" required>
                    </div>
                    <div class="mb-3">
                        <label for="productInfo" class="col-form-label">Ürün Bilgisi:</label>
                        <input type="text" class="form-control" id="productInfo" name="productInfo">
                    </div>
                    <div class="mb-3">
                        <label for="productPic" class="col-form-label">Ürün Fotoğrafı:</label><br>
                        <input type="file" class="form-control-file" id="productPic" name="productPic">
                    </div>
                    <div class="mb-3">
                        <label for="productCategory" class="col-form-label">Ürün Kategorisi:</label>
                        <select class="form-control" id="productCategory" name="productCategory" required>
                            <option value="">Seçiniz</option>
                            @foreach ($categories as $category)
                            <option value="{{ $category->id }}">{{ $category->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="productIngredients" class="col-form-label">Ürün İçerikleri:</label>
                        <input type="text" class="form-control" id="productIngredients" name="productIngredients">
                    <div class="mb-3">
                        <label for="productPrice" class="col-form-label">Ürün Fiyatı:</label>
                        <input type="text" class="form-control" id="productPrice" name="productPrice" required>
                    </div>
                    <button type="submit" class="btn btn-primary btn-submit">Ürün Ekle</button>

and this is my controller:

public function addProduct(Request $request)
    {
        $productName = $request->input('productName');
        $productInfo = $request->input('productInfo');
        $productImage = $request->file('productPic');
        $productCategory = $request->input('productCategory');
        $productIngredients = $request->input('productIngredients');
        $productPrice = (int) $request->input('productPrice');

        $imageName = $request->file('productPic')->getClientOriginalName();

        $path = $request->file('productPic')->storeAs('public/publicImages', $imageName);

        $product = new Products;
        $product->name = $productName;
        $product->info = $productInfo;
        $product->image = $imageName;
        $product->category_id = $productCategory;
        $product->ingredients = $productIngredients;
        $product->price = $productPrice;
        $product->is_active = 1;
        $product->save();

        return redirect('products');
    }

This is returning me error. This is the error: Call to a member function getClientOriginalName() on null

0 likes
5 replies
Pippo's avatar

Form should have the attribute enctype="multipart/form-data" to upload files

1 like
onurzdgn's avatar

@Pippo Yes it is work thank you but now i can't save in public/publicImages folder

Snapey's avatar

@onurzdgn Try the following refactor

    public function addProduct(Request $request)
    {
        $product = new Products;
        $product->name = $request->input('productName');
        $product->info = $request->input('productInfo');
        $product->image = $imageName;
        $product->category_id = $request->input('productCategory');
        $product->ingredients = $request->input('productIngredients');
        $product->price = (int) $request->input('productPrice');
        $product->is_active = 1;
     
        if($request->hasFile('productPic')) {
            $product->image = $request->productPic
                ->storeAs('public/publicImages', $request->productPic->getClientOriginalName());
        }
     
        $product->save();

        return redirect('products');
    }

Only save the image if it is actually on the request

But MAKE SURE you validate the input otherwise a php file could be uploaded and placed into the public folder for free access to your server and content.

No need to create temporary variable names.

I would never allow the user to dictate the file name, plus you will overwrite images if the filename happens to be the same, leading to two products with the same image

You will have a simpler life if you stick with conventions, like singular model names (eg Product)

1 like
onurzdgn's avatar

@Snapey Thank you so much but still not save. I will use yours advice now I am development phase.

onurzdgn's avatar
onurzdgn
OP
Best Answer
Level 2

enctype="multipart/form-data" solution one and second solution is public_path('publicImages')

Please or to participate in this conversation.