Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

foxdevuz's avatar

Laravel not accepting image on API

I'mcurretly working on api for test. I need to upload the image via API on local enviroment, it works but on hosting it doesn't. Here is my code: Route:

Route::post('/v1/admin/{token}/test/create', [ApiTestController::class, 'createTest']);

ApiTestController: (only create test part):

public function createTest(CreateTestRequest $req, $token)
    {
        if (!ApiHelperFunctionsController::check_admin_token($token)) {
            return response()->json(['error' => 'invalid token', 'code' => Response::HTTP_FORBIDDEN]);
        }
        $question_image = null;
        $is_question_file = false;
        if ($req->hasFile('question_image')) {
            $file = $req->file('question_image');
            $extension = $file->getClientOriginalExtension();
            $question_image = Str::random() . '.' . $extension;
            $file->storeAs('/public/images', $question_image);
            $is_question_file = true;
        }
        try {
            Test::create([
                'question' => $req->question,
                'image' => $question_image,
                'answer_1' => $req->answer_1,
                'answer_2' => $req->answer_2,
                'answer_3' => $req->answer_3,
                'correct_answer' => $req->correct_answer,
                'category' => $req->category,
                'is_question_file' => $is_question_file,
            ]);
            return response()->json([
                'ok' => true,
                'message' => 'test created successfully',
                'code' => Response::HTTP_OK,
            ]);
        } catch (Exception $e) {
            return response()->json([
                'ok' => false,
                'code' => Response::HTTP_SERVICE_UNAVAILABLE,
                'message' => $e->getMessage(),
            ]);
        }
    }

Validation rule:

return [
            'question'=>['required', 'string'],
            'answer_1'=>['required'],
            'answer_2'=>['required'],
            'answer_3'=>['required'],
            'correct_answer'=>['required'],
            'category'=>['required'],
        ];

i did dd($req) and it returns:

  +request: Symfony\Component\HttpFoundation\InputBag {#46
    #parameters: array:7 [
      "question" => "A"
      "answer_1" => "123"
      "answer_2" => "321"
      "answer_3" => "213"
      "answer_4" => "312"
      "correct_answer" => "012"
      "category" => "matem-ingliz"
    ]
  }

NOTE: Uploading image is not requred thath's why i didn't add it to valitadion rule

I tried:

Change permission for folder to 775 on hosting

How can i solve this problem?

0 likes
1 reply
foxdevuz's avatar
foxdevuz
OP
Best Answer
Level 3

SOLUTION (if anyone will face this error) You need to send Content-type = multipart/form-data header.

Double check that frontend sending this header!!!

Please or to participate in this conversation.