gpt4 book ai didi

php - 用户注册时上传头像

转载 作者:搜寻专家 更新时间:2023-10-31 20:50:54 24 4
gpt4 key购买 nike

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

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

这里我有 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)
);

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

最佳答案

首先,您真的不想在注册表单上执行此操作。为此创建一个“编辑配置文件”页面。

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

因此,要采取的步骤:

  1. 登录用户将图片上传到其个人资料(使用 )
  2. 您将图像存储在目录中,比如 /images/avatars/$userid.jpg
  3. 每当有人查看用户的个人资料时,您都会插入一个 <img src="/images/avatars/$userid.jpg">标签

或者,为了存储在数据库中:

  1. 登录用户将图片上传到其个人资料(使用 )
  2. 您确定 $filename,例如 $random.$extension
  3. 您将图像存储在目录中,比如 /images/avatars/$filename
  4. 通过设置 avatarurl = $filename 更新用户行
  5. 每当有人查看用户的个人资料时,您都会检索该用户头像的文件名并插入 <img src="/images/avatars/$filename">标签

第二种方法的优点是它独立于文件扩展名,您可以通过为每个图像使用随机 ID 来“隐藏”文件,因此恶意访问者无法猜测和收获所有头像。

关于php - 用户注册时上传头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7739072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com