1

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.

3
  • do you have enctype in <form> ? Commented Oct 25, 2015 at 12:51
  • no..i dont have any enctype in <form>... Commented Oct 25, 2015 at 12:52
  • then this is problem. look at my answer. Commented Oct 25, 2015 at 12:53

1 Answer 1

2

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.

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

2 Comments

thanks ! >< .. can you rougly tell me the usage of enctype ?
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

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.