


How to use PHP to develop a simple online photo album function
How to use PHP to develop a simple online photo album function
Photo album is a common function for displaying pictures online. It can help us browse, share and download pictures easily . This article will introduce how to use PHP to develop a simple online photo album and provide specific code examples.
1. Build a development environment
First, we need to build a development environment. You can choose to install an integrated development environment (IDE) such as XAMPP, WAMP or MAMP, which has built-in Apache server, MySQL database and PHP interpreter.
2. Create a database
Next, we need to create a database to store the album data. You can use phpMyAdmin or other database management tools to create a new database and name it "photo_album" (or any other name you like).
In this database, we can create two tables, one for storing album information and the other for storing photo information. Here, we create two tables: albums and photos.
- albums table
The albums table is used to store basic information of the album, including fields such as album ID, album name, and creation time. The table can be created using the following SQL statement:
CREATE TABLE albums(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- photos table
photos table is used to store specific photo information in each album, including photo ID, photo name, file name, album ID and Upload time and other fields. The table can be created using the following SQL statement:
CREATE TABLE photos(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
filename VARCHAR(255) NOT NULL,
album_id INT(11) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (album_id) REFERENCES albums(id) ON DELETE CASCADE
);
3. Create an album page
Next, we can create a file named "index.php" to display the album list and upload new albums. You can use the following code:
<?php // 导入数据库连接配置文件 require_once 'config.php'; // 查询相册列表 $query = "SELECT * FROM albums"; $result = mysqli_query($conn, $query); ?> <!DOCTYPE html> <html> <head> <title>相册列表</title> <style> .album { margin-bottom: 20px; } </style> </head> <body> <h1 id="相册列表">相册列表</h1> <?php while ($row = mysqli_fetch_assoc($result)): ?> <div class="album"> <h2><?php echo $row['name']; ?></h2> <p>创建时间:<?php echo $row['created_at']; ?></p> <p><a href="view_album.php?id=<?php echo $row['id']; ?>">查看相册</a></p> </div> <?php endwhile; ?> <h2 id="上传新相册">上传新相册</h2> <form action="upload_album.php" method="post"> <label for="name">相册名称:</label> <input type="text" name="name" id="name" required> <input type="submit" value="上传"> </form> </body> </html>
4. Create an album details page
Create a file named "view_album.php" to display the photos in the specified album. You can use the following code:
<?php // 导入数据库连接配置文件 require_once 'config.php'; // 获取相册ID $albumId = $_GET['id']; // 查询相册名称 $query = "SELECT name FROM albums WHERE id = $albumId"; $result = mysqli_query($conn, $query); $albumName = mysqli_fetch_assoc($result)['name']; // 查询相册中的照片列表 $query = "SELECT * FROM photos WHERE album_id = $albumId"; $result = mysqli_query($conn, $query); ?> <!DOCTYPE html> <html> <head> <title>相册详情 - <?php echo $albumName; ?></title> <style> .photo { margin-bottom: 20px; } </style> </head> <body> <h1><?php echo $albumName; ?></h1> <?php while ($row = mysqli_fetch_assoc($result)): ?> <div class="photo"> <h2><?php echo $row['name']; ?></h2> <p>上传时间:<?php echo $row['created_at']; ?></p> <img src="uploads/<?php echo $row['filename']; ? alt="How to use PHP to develop a simple online photo album function" >" alt="<?php echo $row['name']; ?>" width="200"> </div> <?php endwhile; ?> <h2 id="上传新照片">上传新照片</h2> <form action="upload_photo.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="album_id" value="<?php echo $albumId; ?>"> <label for="name">照片名称:</label> <input type="text" name="name" id="name" required> <br> <label for="file">照片文件:</label> <input type="file" name="file" id="file" required> <br> <input type="submit" value="上传"> </form> </body> </html>
5. Upload albums and photos
Next, we need to write the code to upload albums and photos. You can create a file named "upload_album.php" to handle the logic of uploading albums. The code is as follows:
<?php // 导入数据库连接配置文件 require_once 'config.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name']; // 插入相册信息 $query = "INSERT INTO albums(name) VALUES ('$name')"; mysqli_query($conn, $query); // 重定向到相册首页 header('Location: index.php'); }
Similarly, you can create a file named "upload_photo.php" to handle the logic of uploading photos. The code is as follows:
<?php // 导入数据库连接配置文件 require_once 'config.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name']; $albumId = $_POST['album_id']; $filename = $_FILES['file']['name']; // 上传照片文件到服务器 move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $filename); // 插入照片信息 $query = "INSERT INTO photos(name, filename, album_id) VALUES ('$name', '$filename', $albumId)"; mysqli_query($conn, $query); // 重定向到相册详情页面 header("Location: view_album.php?id=$albumId"); }
6. Completion
At this point, a simple online photo album function has been developed. You can visit the "index.php" page in your browser to view the album list. Click to enter the album to view photos and upload new photos. I hope this article can help you use PHP to develop photo album functions!
The above is the detailed content of How to use PHP to develop a simple online photo album function. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
