PHP file upload with extension and size check


/ Published in: PHP
Save to your folder(s)

form enctype="multipart/form-data"


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.