title: how to secure this image upload code
body: <p>I have been hacked during last two weeks and I see in the log files that hacker can upload php shells in registration </p>
  
  <p>this is the form which let user register and save the data to database in the site and the point is when he uploads his avatar he can upload php file</p>
  
  <pre><code>&lt;form method="post" name="regiterationForm" id="regiterationForm" enctype="multipart/form-data"&gt;
  &lt;table width="930" border="0" align="center" cellpadding="2" cellspacing="2"&gt;
  
  &lt;tr&gt;
  &lt;td width="431" valign="top"&gt;&lt;table width="500" border="0" cellspacing="2" cellpadding="2"&gt;
  &lt;tr&gt;
  &lt;td width="215" align="right" class="registerpage_form_credential"&gt;Username:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;
  &lt;input onBlur="checkUserName(this.value);" type="text" name="userName" id="userName" class="register_signup_field" /&gt; &lt;/td&gt;
  &lt;div class="errormsg" id="error_userName" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_userName_val"&gt;Please enter name&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="215" align="right" class="registerpage_form_credential"&gt;Password:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;input type="password" name="userPassword" id="userPassword" class="register_signup_field" /&gt;&amp;nbsp;&lt;/td&gt;
  &lt;div class="errormsg errpass" id="error_userPassword" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_userPassword_val"&gt;Please enter password&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="215" align="right" class="registerpage_form_credential"&gt;Retype Password:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;input type="password" name="userRetypePassword" id="userRetypePassword" class="register_signup_field" /&gt;&amp;nbsp;&lt;/td&gt;
  &lt;div class="errormsg errpass1" id="error_userRetypePassword" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_userRetypePassword_val"&gt;Please enter password&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="215" align="right" class="registerpage_form_credential"&gt;Your real name:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;input type="text" name="userRealName" id="userRealName" class="register_signup_field" /&gt;&amp;nbsp;&lt;/td&gt;
  &lt;div class="errormsg errpass2" id="error_userRealName" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_userRealName_val"&gt;Please enter Name&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="right" class="registerpage_form_credential"&gt;E-mail address:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;input onBlur="checkUserEmail(this.value);" type="text" name="userEmail" id="userEmail" class="register_signup_field" /&gt;
  &amp;nbsp;&lt;/td&gt;
  &lt;div class="errormsg errpass3" id="error_userEmail" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_userEmail_val"&gt;Please enter Email&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="right" class="registerpage_form_credential"&gt;Retype E-mail address:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;input type="text" name="userRetypeEmail" id="userRetypeEmail" class="register_signup_field" /&gt;
  &amp;nbsp;&lt;/td&gt;
  &lt;div class="errormsg errpass4" id="error_userRetypeEmail" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_userRetypeEmail_val"&gt;Please enter Email&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="right" class="registerpage_form_credential"&gt;Country:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;select name="country" id="country" class="register_monthday_selection"&gt;
  &lt;option value=""&gt;Choose a Country&lt;/option&gt;
  &lt;?php for($i=0; $i&lt;count($allCountryName); $i++){ ?&gt;
      &lt;option value="&lt;?php echo $allCountryName[$i]['name']; ?&gt;"&gt;&lt;?php echo $allCountryName[$i]['name']; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;
  &amp;nbsp;&lt;/td&gt;
  &lt;div class="errormsg errpass5" id="error_country" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_country_val"&gt;Please Select Country&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="right" class="registerpage_form_credential"&gt;Date of birth:&lt;/td&gt;
  
  &lt;td class="register_day_bg"&gt;&lt;select name="userDay" id="userDay" class="register_day_selection"&gt;
  &lt;option value=""&gt;Day&lt;/option&gt;
  &lt;?php for($i=1; $i&lt;=31; $i++){ ?&gt;
      &lt;option value="&lt;?php echo $i; ?&gt;"&gt;&lt;?php echo $i; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;&lt;/td&gt;
  
  &lt;td class="register_month_bg"&gt;&lt;select name="userMonth" id="userMonth" class="register_month_selection"&gt;
  &lt;option value=""&gt;Month&lt;/option&gt;
  &lt;?php $month_digit_array = array("1"=&gt;"January","2"=&gt;"February","3"=&gt;"March","4"=&gt;"April","5"=&gt;"May","6"=&gt;"June","7"=&gt;"July","8"=&gt;"August","9"=&gt;"September","10"=&gt;"October","11"=&gt;"November","12"=&gt;"December"); 
      for($i=1; $i&lt;=12; $i++){
  ?&gt;
  &lt;option value="&lt;?php echo $i; ?&gt;"&gt;&lt;?php echo $month_digit_array[$i]; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;&lt;/td&gt;
  
  &lt;td class="register_year_bg"&gt;&lt;select name="userYear" id="userYear" class="register_year_selection"&gt;
  &lt;option value=""&gt;Year&lt;/option&gt;
  &lt;?php $curYear = date('Y');  for($i=$curYear; $i&gt;=1930; $i--){ ?&gt;
      &lt;option value="&lt;?php echo $i; ?&gt;"&gt;&lt;?php echo $i; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td align="right" class="registerpage_form_credential"&gt;I am a:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;select name="gender" id="gender" class="register_monthday_selection"&gt;
  &lt;option value=""&gt;Select an option&lt;/option&gt;
  &lt;option value="Male"&gt;Male&lt;/option&gt;
  &lt;option value="Female"&gt;Female&lt;/option&gt;
  
  &lt;/select&gt;&lt;/td&gt;
  &lt;div class="errormsg errpass6" id="error_gender" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_gender_val"&gt;Please Select Gender&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="right" class="registerpage_form_credential"&gt;Select an Avatar:&lt;/td&gt;
  &lt;td colspan="3" class="registersignupbox_bg"&gt;&lt;input type="file" id="image" name="userAvatar" /&gt;&lt;/td&gt;
  &lt;div class="errormsg errpass7" id="error_image" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_image_val"&gt;Please Select Image&lt;/div&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td colspan="4" style="
      padding-left: 37%;
  "&gt;&lt;div class="g-recaptcha" data-sitekey="6LfhvAkTAAAAAFQyqmN2nf9hXEY1T0jF89SCGNVB"&gt;&lt;/div&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;&lt;/td&gt;
  &lt;td width="455"&gt;
  
  &lt;p&gt;
  
  &lt;iframe width="555" height="312" src="https://www.youtube.com/embed/vkjFm6ClTuw" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
  
  &lt;/p&gt;
  
  &lt;br /&gt;
  &lt;div style="width:468px ;margin: 0 auto; height:60px"&gt;
  
  &lt;script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"&gt;&lt;/script&gt;
  &lt;!-- Header/Footer Small --&gt;
  &lt;ins class="adsbygoogle"
       style="display:inline-block;width:468px;height:60px"
       data-ad-client="ca-pub-5794587985510139"
       data-ad-slot="5064971004"&gt;&lt;/ins&gt;
  &lt;script&gt;
  (adsbygoogle = window.adsbygoogle || []).push({});
  &lt;/script&gt;
  
  &lt;/div&gt;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td colspan="2"&gt;&lt;table width="709" border="0" align="center" cellpadding="2" cellspacing="2" style="margin-top:11px;    margin-top: 15px !important;
  margin-right: 67px;" class="signp"&gt;
  &lt;tr&gt;
  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;td class="contactus_form_credentials"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="21" align="center"&gt;&lt;label&gt;
  &lt;input type="checkbox" name="promotion" id="promotion"/&gt;
  &lt;/label&gt;&lt;/td&gt;
  &lt;td width="442" class="contactus_form_credentials"&gt;I agree that my account will be deleted if my email is not valid&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="center"&gt;&lt;input type="checkbox" name="checkbox2" id="checkbox2"/&gt;&lt;/td&gt;
  &lt;td class="contactus_form_credentials"&gt;I have read and agree to the (example.com) &lt;a href="terms.php" class="termsofuse"&gt;Terms of use&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;td style="cursor:pointer;" onclick="registerFormValidation();"&gt;&lt;img src="images/register_signup_btn.png" width="220" height="108" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  
  
  
  &lt;div class="errormsg errpass7" id="error_captcha" style="display:none;"&gt;&lt;span class="noberr"&gt;&lt;/span&gt;&lt;span id="error_captcha_val"&gt;&lt;/div&gt;
  
  
  
  
  
  
  &lt;/form&gt; 
  </code></pre>
  
  <p>and this the function for registration </p>
  
  <pre><code>function insertData($arr,$files){
      $username  =$arr['userName'];
      $pass      =$arr['userPassword'];
      $realname  =$arr['userRealName'];
      $email     =$arr['userEmail'];
      $country   =$arr['country'];
      $gender    =$arr['gender'];
      $dob       =$arr['userDay'].'-'.$arr['userMonth'].'-'.$arr['userYear'];
      if(self::userNameExit($username) != "not_exist"){
          echo "&lt;script&gt;document.location.href='".HTTP_PATH."register.php'&lt;/script&gt;";
          exit();
      }
      if(self::userEmailExist($email) != "not_exist"){
          echo "&lt;script&gt;document.location.href='".HTTP_PATH."register.php'&lt;/script&gt;";
          exit();
      }
      require_once(COMM_PATH."DatabaseManager.php"); 
      $db= new DatabaseManager();
      $emailcode =  rand().time();
      $sql       =  "Insert into users(username,pass,realname,email,country,dob,gender,lastlogin,register_date,email_varification,email_code)values('".$username."','".md5($pass)."','".$realname."','".$email."','".$country."','".$dob."','".$gender."', now(), now(),'n','".$emailcode."')";
      $result    =  $db-&gt;executeUpdate($sql);
      $user_id   =  $db-&gt;lastInsertId();
      self::autoFriends($user_id);
      self::sentMessage($user_id,$username);
      insertStatusNow($user_id,"user");
      if(isset($files['userAvatar']['name']) &amp;&amp; $files['userAvatar']['name'] !=""){
          $nameOfImage    = time()."_".basename($files['userAvatar']['name']);
          $path           = USER_IMAGE_UPLOAD_PATH.$nameOfImage;
          if(move_uploaded_file($files['userAvatar']['tmp_name'], $path)){
              $sql="UPDATE `users` SET `image` = '".$nameOfImage."' WHERE `Id` =".$user_id;
              $db-&gt;executeUpdate($sql);
              if(isset($arr['promotion']) &amp;&amp; $arr['promotion'] == "promotion_yes"){
                  self::notificationSubmit($user_id,$email);
              }
          }
          return $user_id;
      }
  }
  </code></pre>
  
  <p>I want to know what is wrong with me , the business is going down every day the site hacked </p>
  
  <p>UPDATE ::</p>
  
  <pre><code>    if(isset($files['userAvatar']['name']) &amp;&amp; $files['userAvatar']['name'] !=""){
              $nameOfImage    = $files['userAvatar']['name'];
              $path           = USER_IMAGE_UPLOAD_PATH.$nameOfImage;
  
  
  $fileInfo = new finfo();
  
  $allowedMIMETypes = ['image/bmp', 'image/gif', 'image/jpeg', 'image/png']; //the most common image MIME types, you can add more if you want
  
  if(in_array($fileType = $fileInfo-&gt;file($nameOfImage, FILEINFO_MIME_TYPE, $allowedMIMETypes))){
                  if(move_uploaded_file($files['userAvatar']['tmp_name'], $path)){
                  $sql="UPDATE `users` SET `image` = '".$nameOfImage."' WHERE `Id` =".$user_id;
                  $db-&gt;executeUpdate($sql);
                  if(isset($arr['promotion']) &amp;&amp; $arr['promotion'] == "promotion_yes"){
                      self::notificationSubmit($user_id,$email);
                  }
              }
              return $user_id;
  } 
  
  else {
      die("check you reg");
  }
  return null;
  </code></pre>
  
  <p>I have made the code like that but it let user register but with no photo </p>