Khudadad's avatar

Validation Error When uploading a file

When uploading a pdf file I get this error:

The file must be a file of type: pdf, doc, docx.

My controller method:

public function store(BookRequest $request)
{
      $book = new Book();
    $book->title = $request->input('title');
    $book->isbn = $request->input('isbn');
    $book->author = $request->input('author');
$book->description=$request->input('description');
    if($request->hasFile('file'))
    {
        $file = $request->file('file');
        $fileName = date('Y') . "_" . $file->getClientOriginalName();
        $distination_path = 'books/';
        $file->move($distination_path, $fileName);
        $book->book_path = $distination_path . $fileName;
    }
        $book->user_id = \Auth::user()->id;
    $book->save();
    return redirect('/library')->with('message','کتاب موفقانه آپلود شد.')->withErrors('مشکل رخداد');
}

And the Request:

 public function rules()
{
    return [
        'title'=>'required',
        'author'=>'required',
        'isbn'=>'required',
        'description'=>'required|min:10',
        'file'=>'required|mimes:pdf,doc,docx',
    ];
}

thanks

0 likes
10 replies
coder's avatar

which type of file you are uploading ?? I can see you have added validation for the file which will except only pdf,doc,docx.

mercuryseries's avatar

@Khudadad Are you getting any success for any other files?

For example try to remove the validation on the file, and upload an image in order to check if everything works fine. If that works, so that means your issue is clearly related to the mime type checks. But if that isn't the case, so you may need to check else where in your code in order to find some other issues.

Khudadad's avatar

Thank you @mercuryseries and all I found the mistake it was ($request->$author=...), sorry about that. again thanks. another question is, when I specify the size of file like:

'file'=>'required|max:10000',

is it in Kb or bytes?

kingpabel's avatar

try to use like that

public function rules()
{
    return [
        'title'=>'required',
        'author'=>'required',
        'isbn'=>'required',
        'description'=>'required|min:10',
        'file'=>'required|mimes:application/pdf,application/msword',
    ];
}
1 like

Please or to participate in this conversation.