
Generally, the traditional way we upload pictures is to save the pictures in the local folder of the server. Here I will briefly introduce the method of storing pictures directly into the database.
Mysql database provides the Blob type for storing large amounts of data. There are four types of BLOB that can accommodate data of different sizes.
TinyBlob Maximum 255B
Blob Maximum 65K
MediumBlob Maximum 16M
LongBlob Maximum 4G
It should be noted that the PHP configuration file itself has a size limit for post form data and a size limit for file submission. Therefore, you need to modify php.ini
upload_max_filesize=10M (file submission file size limit)
##post_max_size=10M (form data size limit)
I use php to make a simple registration example. The design of mysql table (field naming is a bit irregular, you need to pay attention to the field naming specifications in actual projects)
Database connection file conn. php
$conn = @mysql_connect('localhost' , 'userName' , 'passWord') or die(mysql_error()); //连接数据库 mysql_query('set names utf8' , $conn); //设置连接字符集 @mysql_select_db('user' , $conn) or die(mysql_error()); //连接数据表
Registration form implementation register.html (select key parts)
<form action="register.php" method="post" enctype="multipart/form-data"> <dl class="admin_login"> <dd class="pic"> <img src="/static/imghwm/default1.png" data-src="img/5.png" class="lazy" alt="" id="preview"/> <a href="javascript:;" class="a-upload"> <input type="file" name="photo" id="" οnchange="imgPreview(this)">上传头像 </a> </dd> <dd class="email_icon"> <input type="text" placeholder="请输入注册邮箱" name="email" class="login_txtbx"> </dd> <dd class="name_icon"> <input type="text" placeholder="请输入帐号昵称" name="name" class="login_txtbx"> </dd> <dd class="phone_icon"> <input type="tel" placeholder="请输入联系号码" name="phone" class="login_txtbx"> </dd> <dd class="school_icon"> <select name="school"> <option value="0">请选择报研学校</option> <option value="1">中南大学</option> <option value="2">电子科技大学</option> <option value="3">上海交通大学</option> <option value="4">华南理工大学</option> </select> </dd> <dd class="pwd_icon"> <input type="password" placeholder="请输入您的密码" name="password" class="login_txtbx"> </dd> <dd class="rpwd_icon"> <input type="password" placeholder="请确认您的密码" class="login_txtbx"> </dd> <dd> <input type="submit" value="立即注册" class="submit_btn"> </dd> </dl> </form>
Upload pictures and registration page register.php
<?php
require('conn.php');
if(empty($_POST)){
require('register.html');
}else{
$image = mysql_real_escape_string(file_get_contents($_FILES['photo']['tmp_name'])); //获取图片
$image_type = $_FILES['photo']['type']; //获取图片格式
$user_name = $_POST['name'];
$user_email = $_POST['email'];
$user_phone = $_POST['phone'];
$user_school = $_POST['school'];
$password = $_POST['password'];
$sqlstr = "insert into user(uname,email,phone,password,school,photo,type) values('".$user_name."','".$user_email."','".$user_phone."','".$password."','".$user_school."','".$image."','".$image_type."')";
@mysql_query($sqlstr) or die(mysql_error()); //执行sql语句,若执行成功,继续下面跳转页面。若执行失败,提示错误信息
header('location:login.php'); //跳转页面
exit();
}
?>Things to note:
1) PHP configuration file size limit
2) When uploading files using form, be sure to Add the attribute content enctype="multipart/form-data", otherwise an exception will be reported when using $_FILES[ 'photo' ] [ ' tmp_name' ] to obtain file information
Recommended tutorial:The above is the detailed content of How to upload pictures to database in php. For more information, please follow other related articles on the PHP Chinese website!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools






