注册期间强制图像上传

时间:2011-08-19 21:21:54

标签: php post html-form

尝试集成两个不同脚本的功能时遇到一些困难。

我有一个登录/注册系统,以后允许用户进入并添加个人资料图片。我希望将个人资料图片图片上传作为注册要求,但我很难整合两者。

这是我在使用php的地方:

 if (isset ($_POST['username'])){

 $username = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']); 
 $gender = preg_replace('#[^a-z]#i', '', $_POST['gender']); 
 $b_m = preg_replace('#[^0-9]#i', '', $_POST['birth_month']); 
 $b_d = preg_replace('#[^0-9]#i', '', $_POST['birth_day']);      
 $b_y = preg_replace('#[^0-9]#i', '', $_POST['birth_year']);      
 $email1 = $_POST['email1'];
 $email2 = $_POST['email2'];
 $pass1 = $_POST['pass1'];
 $pass2 = $_POST['pass2'];
 $user_pic = $_POST['user_pic'];

 include_once "scripts/connect_to_mysql.php";
 $emailCHecker = mysql_real_escape_string($email1);
 $emailCHecker = str_replace("`", "", $emailCHecker);
 $sql_uname_check = mysql_query("SELECT username FROM myMembers WHERE username='$username'"); 
 $uname_check = mysql_num_rows($sql_uname_check);
 $sql_email_check = mysql_query("SELECT email FROM myMembers WHERE email='$emailCHecker'");
 $email_check = mysql_num_rows($sql_email_check);

 if ((!$username) || (!$gender) || (!$b_m) || (!$b_d) || (!$b_y) || (!$email1) || (!$email2) || (!$pass1) || (!$pass2) || (!$user_pic)) {

 $errorMsg = 'ERROR: You did not submit the following required information:<br /><br />';

 if(!$username){ 
   $errorMsg .= ' * User Name<br />';
 } 
 if(!$gender){ 
   $errorMsg .= ' * Gender: Confirm your sex.<br />';
 }  
 if(!$b_m){ 
   $errorMsg .= ' * Birth Month<br />';      
 }
 if(!$b_d){ 
   $errorMsg .= ' * Birth Day<br />';        
 } 
 if(!$b_y){ 
   $errorMsg .= ' * Birth year<br />';        
 }      
 if(!$email1){ 
   $errorMsg .= ' * Email Address<br />';      
 }
 if(!$email2){ 
   $errorMsg .= ' * Confirm Email Address<br />';        
 }  
 if(!$pass1){ 
   $errorMsg .= ' * Login Password<br />';      
 }
 if(!$pass2){ 
   $errorMsg .= ' * Confirm Login Password<br />';        
 }  
 if(!$user_pic){ 
   $errorMsg .= ' * Add a Profile Picture<br />';        
 }  

 } else if ($email1 != $email2) {
          $errorMsg = 'ERROR: Your Email fields below do not match<br />';
 } else if ($pass1 != $pass2) {
          $errorMsg = 'ERROR: Your Password fields below do not match<br />';    
 } else if (strlen($username) < 4) {
           $errorMsg = "<u>ERROR:</u><br />Your User Name is too short. 4 - 20 characters please.<br />"; 
 } else if (strlen($username) > 20) {
           $errorMsg = "<u>ERROR:</u><br />Your User Name is too long. 4 - 20 characters please.<br />"; 
 } else if ($uname_check > 0){ 
          $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.<br />"; 
 } else if ($email_check > 0){ 
          $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside of our system. Please use another.<br />"; 
 } else if ($_FILES['fileField']['tmp_name'] != "") { 
        $maxfilesize = 51200; // 51200 bytes equals 50kb
        if($_FILES['fileField']['size'] > $maxfilesize ) { 

                    $error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';
                    unlink($_FILES['fileField']['tmp_name']); 

        } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {

                    $error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';
                    unlink($_FILES['fileField']['tmp_name']); 
        } else { 
                    $newname = "image01.jpg";
                    $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/".$newname);
        }

} {

 $email1 = mysql_real_escape_string($email1);
 $pass1 = mysql_real_escape_string($pass1);
 $db_password = md5($pass1); 
 $full_birthday = "$b_y-$b_m-$b_d";
 $ipaddress = getenv('REMOTE_ADDR');

 $sql = mysql_query("INSERT INTO myMembers (username, gender, birthday, email, password, ipaddress, sign_up_date) 
 VALUES('$username','$gender','$full_birthday','$email1','$db_password', '$ipaddress', now())")  
 or die (mysql_error());
 $id = mysql_insert_id();
 mkdir("members/$id", 0755);
 }

 } else { // if the form is not posted with variables, place default empty variables so no warnings or errors show

  $errorMsg = "";
  $username = "";
  $gender = "";
  $b_m = "";
  $b_d = "";
  $b_y = "";
  $email1 = "";
  $email2 = "";
  $pass1 = "";
  $pass2 = "";
  $user_pic = "";
}

然后是相应的html表单:

<table class="table_f" width="100%" cellpadding="3">
<form action="register.php" method="post" enctype="multipart/form-data">

      <tr>
        <td colspan="2"><font color="#94A0D1"><?php print "$errorMsg"; ?></font></td>
      </tr>       
      <tr>
        <td><h11>Add Profile Picture: </h11></td>         
          <td width="61"><?php echo $user_pic; ?></td>
          <td width="521"><input name="fileField" type="file" class="formFields" id="fileField" size="42" />
          50 kb max </td>
      </tr> 
      <tr>
        <td><input type="submit" style="color: #a2a2a2; font-family: helvetica; font-size: 11px; letter-spacing: 1px" name="Submit" value="Register" />
        </td>
      </tr>
    </form>
  </table>

如果有人对我可能遗失的内容有任何想法,我会非常感激。我一直盯着这个,以至于我可能遗漏了一些明显的东西。

提前感谢任何建议。

1 个答案:

答案 0 :(得分:0)

看起来好像你有一个名为user_pic的输入,这是你希望得到的POST变量。您已将文件选择器命名为fileField,根据您的其余代码,该文件选择器应为user_pic