在他/她注册时为用户上传头像

时间:2011-10-12 11:05:18

标签: php sql

我正在创建一个链接共享网站,我希望注册的用户上传一个头像并在整个网站中使用该头像。

我到目前为止用户可以注册,但找不到让他/她拥有头像的方法。

这里我有signup.php文件,以便你可以看到我的意思。

<?php
    include 'connect.php';
    include 'header.php';

    echo '<h3>Register</h3><br />';

    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {
      /*the form hasn't been posted yet, display it
  note that the action="" will cause the form to post to the same page it is on */

    echo '<form method="post" action="">
    <b>Username: </b><input type="text" name="user_name" /><br/><br/>
    <b>Password: </b><input type="password" name="user_pass"><br/><br/>
    <b>Confirm assword: </b><input type="password" name="user_pass_check"><br/>      <br/>
    <b>E-mail: </b><input type="email" name="user_email"><br/><br/>

          ///////////////////////////////////////////////////////////
          ///////// must I use <input type="file"> here???? /////////
          ///////// and how do I put it in the database???? /////////
          /////////////////////////////////////////////////////////// 

    <input type="submit" value="Join" />
 </form>';
    }
    else
    {
    /* so, the form has been posted, we'll process the data in three steps:
    1.  Check the data
    2.  Let the user refill the wrong fields (if necessary)
    3.  Save the data 
*/
$errors = array(); /* declare the array for later use */

if(isset($_POST['user_name']))
{
    //the user name exists
    if(!ctype_alnum($_POST['user_name']))
    {
        $errors[] = 'The username can only contain letters and digits.';
    }
    if(strlen($_POST['user_name']) > 30)
    {
        $errors[] = 'The username cannot be longer than 30 characters.';
    }
}
else
{
    $errors[] = 'The username field must not be empty.';
}


if(isset($_POST['user_pass']))
{
    if($_POST['user_pass'] != $_POST['user_pass_check'])
    {
        $errors[] = 'The two passwords did not match.';
    }
}
else
{
    $errors[] = 'The password field cannot be empty.';
}

if(!empty($errors)) 

    /*check for an empty array, if there are errors, they're in this array (note the !     operator)*/
{
    echo 'Uh-oh.. a couple of fields are not filled in correctly..<br /><br />';
    echo '<ul>';
    foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */
    {
        echo '<li>' . $value . '</li>'; /* this generates a nice error list */
    }
    echo '</ul>';
}
else
{



    //the form has been posted without, so save it
    //notice the use of mysql_real_escape_string, keep everything safe!
    //also notice the sha1 function which hashes the password
    $sql = "INSERT INTO
                users(user_name, user_pass, user_email ,user_date, user_level)
            VALUES('" . mysql_real_escape_string($_POST['user_name']) . "',
                   '" . sha1($_POST['user_pass']) . "',
                   '" . mysql_real_escape_string($_POST['user_email']) . "',
                    NOW(),
                    0)";

    $result = mysql_query($sql);
    if(!$result)
    {
        //something went wrong, display the error
        echo 'Something went wrong while registering. Please try again   later.';
        //echo mysql_error(); //debugging purposes, uncomment when needed
    }
    else
    {
        echo 'Succesfully registered. You can now <a href="signin.php">sign  in</a> and start sharing links.';
    }
}
}

include 'footer.php';
?>

这是我的数据库文件,所以你可以告诉我如何在数据库中添加头像

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);

CREATE TABLE categories (  
cat_id          INT(8) NOT NULL AUTO_INCREMENT,  
cat_name        VARCHAR(255) NOT NULL,  
cat_description     VARCHAR(255) NOT NULL,  
UNIQUE INDEX cat_name_unique (cat_name),  
PRIMARY KEY (cat_id)  
);

CREATE TABLE topics (  
topic_id        INT(8) NOT NULL AUTO_INCREMENT,  
topic_subject       VARCHAR(255) NOT NULL,  
topic_date      DATETIME NOT NULL,  
topic_cat       INT(8) NOT NULL,  
topic_by        INT(8) NOT NULL,  
PRIMARY KEY (topic_id)  
); 

CREATE TABLE posts (  
post_id         INT(8) NOT NULL AUTO_INCREMENT,  
post_content        TEXT NOT NULL,  
post_date       DATETIME NOT NULL,  
post_topic      INT(8) NOT NULL,  
post_by     INT(8) NOT NULL,  
PRIMARY KEY (post_id)  
);

当用户注册时,如何在数据库中添加头像?

3 个答案:

答案 0 :(得分:3)

首先,你真的不想在报名表上这样做。为此创建“编辑个人资料”页面。

其次,如果不再次开始讨论,您可能不希望将头像图像存储在数据库中,而是将其存储为文件并将文件名存储在用户/配置文件表中(或使用用户名或ID作为文件名)。

所以,采取的步骤:

  1. 登录用户将图片上传到其个人资料(使用)
  2. 您将图像存储在目录中,例如/images/avatars/$userid.jpg
  3. 每当有人查看用户的个人资料时,您都会插入<img src="/images/avatars/$userid.jpg">代码
  4. 或者,存储在数据库中:

    1. 登录用户将图片上传到其个人资料(使用)
    2. 您确定$ filename,例如$ random。$ extension
    3. 您将图像存储在目录中,例如/images/avatars/$filename
    4. 通过设置avatarurl = $ filename
    5. 更新用户行
    6. 每当有人查看用户的个人资料时,您都会检索该用户的头像的文件名并插入<img src="/images/avatars/$filename">标记
    7. 第二种方法的优势在于它独立于文件扩展名,您可以通过为每个图像使用随机ID来“隐藏”文件,因此恶意访问者无法猜测并收获所有头像。

答案 1 :(得分:0)

您不会将实际图像保存到数据库中,只保存文件名。

然后,您只需在表单中添加一个文件选择元素,然后使用它上传图像并将文件名保存在数据库中。

我个人喜欢使用uploadify(http://www.uploadify.com/)这样做,这使得用户的过程更加便宜。

答案 2 :(得分:0)

Plupload是将文件上传到服务器的另一个示例。我已经使用了uploadify,swfupload和其他一些我记不住的了。 Plupload是那里最好的。

plupload