0

I am new to WordPress and Php development. I have a general idea of how this is supposed to work but need help with the syntax.

So I have a form which has an input of type file inside a form

<input type="file" name="cv" />

I am only accepting files of type doc, docx and pdf and they need to be less than 2MB in size. Just need to get some direction of how to post a form with this field on it, do some basic validation checks on size and type before I mail this out. I have already looked at Wp_mail and it seems straight forward enough, but I would like to know what the best practice is in getting this done.

NOTE: I do not want to use a plugin for this - consider this a learning exercise and wanting to know how this is done instead of always defaulting to the answer that I will use a plugin.

haraman
  • 2,744
  • 2
  • 27
  • 50
Gjohn
  • 1,261
  • 1
  • 8
  • 12
  • Less than 2KB? I typed `this is a test` into Wordpad, saved it as a `.docx`, and the filesize was 1.9KB. You might want to change that to 2MB. – Ryan Willis Dec 09 '15 at 15:41

1 Answers1

1

Validating a file type would require additional coding on the server side, as any file can be given those extensions, but here are some PHP examples:

Make sure your form has enctype='multipart/form-data'

$file = $_FILES['cv'];
$size_limit = 2 * 1024 * 1024; // 2MB size limit
$file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if($file_ext == 'docx' || $file_ext == 'doc' || $file_ext == 'pdf'){
    if($size_limit >= $file['size']){
       //proceed with upload
    }        
}

Useful links:

Codular PHP File Uploads

File Extension

Additionally, you may want to check against $file['type'] with its corresponding mime-type.

Community
  • 1
  • 1
Ryan Willis
  • 624
  • 3
  • 13