Home  >  Article  >  Backend Development  >  How to use PHP to develop a simple online photo album function

How to use PHP to develop a simple online photo album function

WBOY
WBOYOriginal
2023-09-21 08:21:471582browse

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.

  1. 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
);

  1. 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>相册列表</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>上传新相册</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>上传新照片</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!

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