0

I tried to store the image in DB along with some other data in another table ,but I get

"message": "Undefined variable $filenameA"

        //for picture refrences
        if (!empty($request->picture_a)) {
            $fileA = $request->file('picture_a');
            $extensionA = $fileA->getClientOriginalExtension();
            $filenameA = 'picture_a' . '_' . time() . '.' . $extensionA;
            $fileA->storeAs('uploads/picture_refrences', $filenameA);
            $data['picture_a'] = 'public/uploads/' . $filenameA;
        }

        if (!empty($request->picture_b)) {
            $fileB = $request->file('picture_b');
            $extensionB = $fileB->getClientOriginalExtension();
            $filenameB = 'picture_b' . '_' . time() . '.' . $extensionB;
            $fileB->storeAs('uploads/picture_refrences', $filenameB);
            $data['picture_b'] = 'public/uploads/' . $filenameB;
        }
        if (!empty($request->picture_c)) {
            $fileC = $request->file('picture_c');
            $extensionC = $fileC->getClientOriginalExtension();
            $filenameC = 'picture_c' . '_' . time() . '.' . $extensionC;
            $fileC->storeAs('uploads/picture_refrences', $filenameC);
            $data['picture_c'] = 'public/uploads/' . $filenameC;
        }

This is what I used and it was working when I used it to store in public directory now since I tried to store in storage it does not upload images enter image description here

Zenis
  • 11
  • 3
  • 1
    Is there anywhere in your code the variable `$filenameA` is referenced? I don't understand why the error `"Undefined variable $filenameA"` is showing. – aceraven777 Dec 02 '22 at 05:47
  • //save the data to the database $pictureRefrence = new Picture_refrence(); $pictureRefrence->record_id = $maxid; $pictureRefrence->desc_a = $request->desc_a; $pictureRefrence->picture_a = $filenameA; $pictureRefrence->desc_b = $request->desc_b; $pictureRefrence->picture_b = $filenameB; $pictureRefrence->desc_c = $request->desc_c; $pictureRefrence->picture_c = $filenameC; $pictureRefrence->save(); $filenameA is used to store in db – Zenis Dec 02 '22 at 06:00
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – steven7mwesigwa Dec 02 '22 at 06:22
  • [Notice: Undefined variable](https://stackoverflow.com/a/12778634/7376590) – steven7mwesigwa Dec 02 '22 at 06:23

1 Answers1

-1

you can use this code, it is working and tested

$image = $request->file('picture_a');
        if (isset($image))
        {
            $currentDate = Carbon::now()->toDateString();
            $imageName = $currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
            if (!file_exists('uploads/photos')){
                mkdir('uploads/photos',0777,true);
            }
            $image->move('uploads/photos',$imageName);
        }

  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – steven7mwesigwa Dec 02 '22 at 06:25
  • [Is “try this” bad practice?](https://meta.stackexchange.com/questions/196187/is-try-this-bad-practice/196191#196191) – steven7mwesigwa Dec 02 '22 at 06:36
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 03 '22 at 08:05