Posted By

mikael12 on 10/02/10


Tagged

upload


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

bobbym245
benediktvaldez
swagat
silverskymedia
Groobox
Priestd09


PHP file upload with extension and size check


 / Published in: PHP
 

form enctype="multipart/form-data"

  1. if($_FILES['image']['name'] != '')
  2. {
  3. $filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $_FILES['image']['name']);
  4. $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
  5.  
  6. if (! preg_match('/\.(jpg|gif|png)/i', $ext))
  7. echo 'Filetype not supported.';
  8.  
  9. if ($_FILES['image']['size'] > 1000000)
  10. echo 'File size too high, 1MB is allowed.';
  11.  
  12. move_uploaded_file($_FILES['image']['tmp_name'], PATH . '/'.$filename);
  13. }

Report this snippet  

You need to login to post a comment.