0

I'm trying to upload an image using the API. Even though, after running the first test, the database just stored the directory and not the file-name, worst part, it stored all directories that come before the one that is needed to store the image.

What is being stored

The code I'm using for the store is the following one:

public function store(Request $request)
    {

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

        if($image == null){
            $imagesDir = 'subjectImgs/';
            $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
            $fileDir = $images[array_rand($images)];
            $path = $fileDir;
        } else {
            $path = $image->storeAs('uploads/images/store/',  $image->getClientOriginalName(), 'public');
        }

        $subject = new Homework([
        'subject_id' => $request->subject_id,
        'user_id' => auth()->user()->id, 
        'title' => $request->name,
        'image' => $path,
        'progress' => $request->progress,
        'description' => $request->description,
        'duedate' => $request->date
        ]);

        $subject->save();

        return response()->json([
        "code" => 200,
        "message" => "Homework added successfully"
        ]);
    }

I'm uploading from a mobile device, I believe this info is also important (?) Same issue when in the simulator.

2 Answers 2

1

You are saving $path into $subject->image that is equal to public_path() . '/uploads/images/store/';. You should add $file->getClientOriginalName() if you want the filename :

$subject = new Homework([
   ...
   'image' => $path . $file->getClientOriginalName(),
   ...
]);

Sign up to request clarification or add additional context in comments.

Comments

1

You are settings 'image' => $path, and before that $path = public_path() . '/uploads/images/store/'; which yields the expected results.

Now if you want to only save from a form input, you can use the following code:


        $image = $request->file('image');
        $path = $image->storeAs('uploads/images/store/',  $image->getClientOriginalName(), 'public');

Where 'public' is the default public filesystem as described in the doc here: https://laravel.com/docs/7.x/filesystem#the-public-disk

7 Comments

This seems to work, but when I try to upload an image from mobile, nothing gets stored.
Everything is happening on server-side in this code, try to debug the frontend regarding your mobile issue
Let me upload the updated code I got. I've tried to debug using Insomnia (its better than postman), still it didn't store the image. It's now the code the one that is troubling.
have you run the command php artisan storage:link before using the public filesystem?
Yup,I did run it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.