开发者

Forcing Image Upload During Registration

Facing a bit of difficulty when trying to integrate the functionality of two different scripts.

I have a login / registration system that later allows the user to go in and add a profile picture. I want to include the profile image picture upload as a requirement for registration, but am facing difficulty integrating the two.

Here's where I am at with the 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 = "";
}

And then the corresponding html form:

<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>

If anyone has any ideas about what I might be missing I would really appreciate it. I have been staring at this for so long that I am probably missing something obvious.

Thanks in advance for any advice.


It doesn't look as if you have an input called user_pic which is what you are looking to get as a POST variable. You have named your file picker as fileField, which should be user_pic according to the rest of your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜