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

NoneNameDeveloper's avatar

How to chack uploading file type | Laravel 5.8

I going to upload files to specific catalogs by type. For example If the file type is .zip or .rar, then upload it to the storage/archives/ directory or else if file type is .mp4 then upload it to storage/videos/ folder. So I have like this controller. But it doesn't check file type:

 
$data =  \Input::except(array('_token')) ;
        $rule=array(
            'file_name' => 'required|mimes:zip,jpg'            
        );
        $validator = \Validator::make($data,$rule);
        if ($validator->fails())
        {
            return redirect()->back()->withErrors($validator->messages());
        } 
        $inputs = $request->all();
        $getFileTyper = $request->file('file_name')->getMimeType();
$st_file = $request->file('file_name');
        $st_file = Input::file('file_name'); 
        if(isset($st_file))
         { 
         $filename = time() . '12.' . $st_file->getClientOriginalName();
         $zipformat = public_path('files/archive/'); 
         $st_file->move($zipformat,$filename); 
         $zipname = $filename; 
         $files->file_name = $filename;
         $files->save();
     if(!empty($inputs['id'])){
        if ($getFileTyper == 'application/zip') {
            \Session::flash('warning', "You successfully change file ZIP information's");
         }
         else {
            \Session::flash('warning', "You successfully change file information's");
        }
        return \Redirect::back();
    }
    else {
        \Session::flash('warning', "You successfully uploaded new file");
        return \Redirect::back();
        }
    }
     else
     {
        //$zipname = "";
        \Session::flash('warning', "Please choose file for upload");
        return \Redirect::back();
     }

Here I tried to check the file type, but it did not work. I checked file type with $getFileTyper = $request->file('file_name')->getMimeType(); But when I dump it, then it returned true answer dd($request->file('file_name')->getMimeType()); returned "application/zip" answer. So how split and check file types while uploading?

0 likes
1 reply
chatty's avatar
chatty
Best Answer
Level 4

i think UploadedIlfe class has the method called extension @nonenamedeveloper

$request->file('file_name')->extension()// returns a file extension
2 likes

Please or to participate in this conversation.