Home  >  Article  >  Database  >  PHP development practice: Use PHP and MySQL to implement image upload and display functions

PHP development practice: Use PHP and MySQL to implement image upload and display functions

王林
王林Original
2023-07-02 09:39:061078browse

PHP development practice: Using PHP and MySQL to implement image upload and display functions

Introduction:
In the era of modern social media and e-commerce, implementing image upload and display functions is very common in website development One of the needs. Implementing such functionality using PHP and MySQL is relatively simple and efficient. This article will introduce how to use PHP and MySQL to implement basic image upload and display functions, and come with corresponding code examples.

  1. Preparation work:
    Before we start, we need to ensure that PHP and MySQL have been installed in the system, and a database and corresponding data table have been created to store image information.

First, create a database named "images" and create a data table named "gallery" to store image-related data.

The data table structure is as follows:
CREATE TABLE gallery (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
filename varchar(255) NOT NULL,
created_at datetime NOT NULL,
PRIMARY KEY ( id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  1. Image upload function:
    Next, we will implement a simple image upload function that allows users to choose and Upload images to the server.

e6ee408a49b1a477ad34c0365e38fdd9

The above code will save the pictures uploaded by the user to the "uploads" folder of the server, and insert the relevant information of the pictures into the database table.

  1. Image display function:
    Next, we will display the previously uploaded images and display them to the user in the form of an image grid.

4efa0e76ca8196a5078164260a6872d2query($sql);

if($result->num_rows > 0){

while($row = $result->fetch_assoc()){
    echo "<div class='image-box'>";
    echo "<img src='uploads/" . $row['filename'] . "' alt='" . $row['title'] . "'>";
    echo "<p>" . $row['title'] . "</p>";
    echo "</div>";
}

} else{

echo "没有上传的图片。";

}

$connect->close();
?>

The above code will query the image information saved in the database and use the image grid's form is displayed to the user.

Conclusion:
By using PHP and MySQL, we can easily implement image upload and display functions, adding a better user experience to the website. This article provides basic code examples that readers can extend and optimize according to their own needs. I hope this article can help you implement image upload and display functions in PHP development.

The above is the detailed content of PHP development practice: Use PHP and MySQL to implement image upload and display functions. 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