How to use PHP to implement a simple online image upload and display system

PHPz
Release: 2023-09-25 09:34:01
Original
1658 people have browsed it

How to use PHP to implement a simple online image upload and display system

How to use PHP to implement a simple online image upload and display system

The image upload and display system is one of the commonly used functions of modern websites and is used during the development process PHP can quickly implement this function. This article will introduce how to use PHP to write a simple online image upload and display system, and provide specific code examples.

1. Create database and table

First, we need to create a database and table to store the uploaded image information. Use the following SQL statement to create a table named "images" and set the id, filename, and uploaded_date columns.

CREATE DATABASE image_uploads;
USE image_uploads;
CREATE TABLE images (
    id INT AUTO_INCREMENT PRIMARY KEY,
    filename VARCHAR(255) NOT NULL,
    uploaded_date DATETIME
);
Copy after login

2. Create an upload form page

The next step is to create a form page for uploading images. Create a file named "upload.html" and use the following code:

<!DOCTYPE html>
<html>
<head>
    <title>图片上传</title>
</head>
<body>
    <form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="image">
        <input type="submit" value="上传">
    </form>
</body>
</html>
Copy after login

This form page contains a file selection box and an upload button. When the user selects an image file and clicks the upload button, the form Will be submitted to the "upload.php" page for processing.

3. Create an upload script

Create a file named "upload.php" and use the following code:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_FILES["image"])) {
        $file = $_FILES["image"];
        $filename = $file["name"];
        $target = "uploads/" . basename($filename);
        $uploaded_date = date("Y-m-d H:i:s");

        if (move_uploaded_file($file["tmp_name"], $target)) {
            $con = new mysqli("localhost", "root", "password", "image_uploads");
            $stmt = $con->prepare("INSERT INTO images (filename, uploaded_date) VALUES (?, ?)");
            $stmt->bind_param("ss", $filename, $uploaded_date);
            $stmt->execute();
            $stmt->close();
            $con->close();

            echo "图片上传成功";
        } else {
            echo "图片上传失败";
        }
    }
}
?>
Copy after login

This script first checks whether a file has been Upload and get the file's name, destination path, and upload date. Then, the file is moved to the specified target path and the file information is stored in the database.

4. Create a picture display page

Finally, we need to create a page to display the uploaded pictures. Create a file named "gallery.php" and use the following code:

<!DOCTYPE html>
<html>
<head>
    <title>图片展示</title>
</head>
<body>
    <?php
    $con = new mysqli("localhost", "root", "password", "image_uploads");
    $result = $con->query("SELECT * FROM images ORDER BY uploaded_date DESC");

    while ($row = $result->fetch_assoc()) {
        echo '<img  src="uploads/' . $row["filename"] . '"    style="max-width:90%"  style="max-width:90%" alt="How to use PHP to implement a simple online image upload and display system" >';
    }

    $con->close();
    ?>
</body>
</html>
Copy after login

This page uses a database query to obtain all uploaded image information and displays each image on the page through a loop.

5. Final Notes

Please make sure to create a folder named "uploads" on the server to store uploaded images. And give the folder appropriate write permissions.

The above are the steps and code examples for using PHP to implement a simple online image upload and display system. You can modify and extend the code according to actual needs to create more powerful image uploading and display functions.

The above is the detailed content of How to use PHP to implement a simple online image upload and display system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template