1
votes

currently i'm working on a application where user can upload a file to my system. I've followed everything from the doc provided by laravel, below is my code.

In my HTML, my image input

<input type="file" name="thread_image">

In my controller, i have done the validation as below

$validator = Validator::make($request::all(),
            [
                'thread_title' => 'required|max:100',
                'thread_remark' => 'max:1000',
                'thread_image' => 'required|mimes:jpeg,jpg,png',
            ],
            [
                'thread_title.required' => 'Please fill in thread title.',
                'thread_title.max' => 'Thread title has exceeded 100 characters.',
                'thread_remark.max' => 'Thread remark has exceeded 1000 characters.',
                'thread_image.required' => 'Please select an image to post this thread.',
                'thread_image.mimes' => 'Please select an correct image with the format of jpeg,jpg,png.',
            ]);

        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        }

Other input have validated correctly, exact the image. Am i doing anything wrong? I've try to upload jpeg,jpg and png, none of them have pass the validation.

1
do you have enctype in <form> ?fico7489
no..i dont have any enctype in <form>...Darren Lau
then this is problem. look at my answer.fico7489

1 Answers

2
votes

Do you have enctype="multipart/form-data" attribute in form tag

<form action="script.php" method="post" enctype="multipart/form-data">

The enctype attribute specifies how the form-data should be encoded when submitting it to the server. You need this for submiting images.