Home  >  Article  >  Backend Development  >  Second-hand recycling website developed with PHP realizes user avatar changing function

Second-hand recycling website developed with PHP realizes user avatar changing function

PHPz
PHPzOriginal
2023-07-01 18:43:43851browse

The second-hand recycling website developed by PHP realizes the function of changing the user's avatar

With the rise of the second-hand trading market, more and more people are beginning to buy and sell their unwanted items through second-hand recycling websites. As a user-friendly second-hand recycling website, it is very important to provide users with a more personalized experience. A simple and practical function is the user avatar changing function. In this article, we will introduce how to implement this function through PHP development.

First of all, we need to get the user's avatar when the user registers, which can be achieved through an upload form. The following is a simple sample code:

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="avatar">
    <input type="submit" value="上传头像">
</form>

In this example, we use a form to allow users to select and upload their own avatar pictures. The form's enctype attribute is set to "multipart/form-data" to allow file uploads.

Next, we need to write an upload.php file to process the avatar uploaded by the user. The following is a simple code example:

$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["avatar"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));

// 检查文件的大小
if ($_FILES["avatar"]["size"] > 500000) {
    echo "文件太大,请选择小于500KB的图片。";
    $uploadOk = 0;
}

// 允许的文件格式
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "请上传jpg, jpeg, png或gif格式的图片。";
    $uploadOk = 0;
}

// 如果$uploadOk为0,文件上传失败
if ($uploadOk == 0) {
    echo "文件上传失败。";
} else {
    if (move_uploaded_file($_FILES["avatar"]["tmp_name"], $targetFile)) {
        echo "头像上传成功。";
    } else {
        echo "头像上传失败。";
    }
}

In this example, we first specify an upload directory $targetDir, and then pass $_FILES["avatar"][" name"]Get the file name of the uploaded file and connect it with the upload directory to get the complete target file path$targetFile. Then we obtain the suffix name of the uploaded file through the pathinfo() function for subsequent format checking.

Next, we check whether the size of the uploaded file exceeds the limit, and check whether the file format meets the requirements. If the file size or format does not meet the requirements, a corresponding error message will be output. If everything is OK, we use the move_uploaded_file() function to move the uploaded file from the temporary directory to the specified target file path.

Through the above code, we have successfully implemented the function of users uploading avatars. However, the avatar file names uploaded by users are all random strings, which is inconvenient for subsequent use of the system. Next, we also need to provide an interface for users to change their avatar.

The following is a simple code example:

<form method="post" action="change_avatar.php" enctype="multipart/form-data">
    <input type="file" name="new_avatar">
    <input type="submit" value="更换头像">
</form>

In this example, we also use a form to allow users to select and upload a new avatar image. Note that the form's enctype attribute is also set to "multipart/form-data".

Next, we need to write a change_avatar.php file to handle the new avatar uploaded by the user. The following is a simple code example:

$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["new_avatar"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));

// 检查文件的大小
if ($_FILES["new_avatar"]["size"] > 500000) {
    echo "文件太大,请选择小于500KB的图片。";
    $uploadOk = 0;
}

// 允许的文件格式
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "请上传jpg, jpeg, png或gif格式的图片。";
    $uploadOk = 0;
}

// 如果$uploadOk为0,文件上传失败
if ($uploadOk == 0) {
    echo "文件上传失败。";
} else {
    // 删除原有的头像文件
    $oldAvatar = 'uploads/old_avatar.jpg'; // 假设用户原有的头像文件名为old_avatar.jpg
    if (file_exists($oldAvatar)) {
        unlink($oldAvatar);
    }

    if (move_uploaded_file($_FILES["new_avatar"]["tmp_name"], $targetFile)) {
        echo "头像更换成功。";
    } else {
        echo "头像更换失败。";
    }
}

In this example, similar to the code logic for uploading avatars, we also check and process new avatars uploaded by users. The difference is that before uploading a new avatar, we first delete the user's original avatar file.

Through the above code, we have successfully implemented a simple user avatar replacement function. Users can now change their profile picture to suit their preferences, allowing for a more personalized experience.

Of course, this is just a basic example, and developers can expand and optimize it according to actual needs. For example, more image format checking and user permission verification functions can be added to ensure that the files uploaded by users are safe and reliable.

The above is the detailed content of Second-hand recycling website developed with PHP realizes user avatar changing function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn