简体   繁体   中英

laravel check file extension in upload form

this is the html form:

<form method = "post" action="...">
   <input type="text" name="subject" />
   <input type="file" type="files['new']['file']"/>
   <input type="text" type="files['new']['name']"/>
</form>

Now I try read in server (laravel).

   public function chechkForm(request $request){
      $input = $request->all();
      dd(input['files']['new']['file' );
   }

the result is:

UploadedFile {#1356 ▼
  -test: false
  -originalName: "GK-Footer-Logo.png"
  -mimeType: "image/png"
  -error: 0
  #hashName: null
  path: "C:\Users\ali\AppData\Local\Temp"
  filename: "phpC3BF.tmp"
  basename: "phpC3BF.tmp"

Now I try these to print mimetype:

$input['new0']['content']['mimeType']

or

$input['new0']['content']->mimeType

or

$input['new0']['content']->mimeType()

what is my wrong? I get for all of them 'error'!

if you want to know the extension and mime-type

you can use getClientOriginalExtension method and getClientMimeType

or other you wanna use you can find it here other methods

simple usage and more laravel way

the form you can set up like this

<form method = "post" action="..." enctype="multipart/form-data">
... other input ...
<input type="file" name="picture"/>
.... other input ....
</form>

in controller you can process with helper file('field_name');

public function checkForm(Request $request){
  $file = $request->file('picture');
  dd( $file->getClientOriginalExtension() );
  dd( $file->getClientMimeType() );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM