With the popularity and development of the Internet, content management systems (CMS) have become an important tool for the construction of many websites. In CMS, the image and display management module is an essential part, realizing the display and management of multimedia content on the website. In the process of developing CMS, using PHP language to develop picture and display management modules is one of the common choices. This article will introduce how to use PHP to develop image and display management modules in CMS.
1. Development of image management module
Image upload is a core function in the image management module in CMS. To implement image upload, you need to first create an upload form through which users can upload images. The PHP code is as follows:
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form>
The "enctype" attribute in the upload form must be set to "multipart/form-data", otherwise the file cannot be uploaded.
After completing the image upload, you need to save the image to the server. Use PHP's "move_uploaded_file" function to save images:
$target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
Among them, $target_dir is the target directory where the image is saved, the basename function obtains the file name of the uploaded image, and $_FILES "fileToUpload" is the temporary file name of the uploaded image. , the move_uploaded_file function moves the temporary file to the target directory.
After saving the picture, you need to display the picture on the website and provide management functions. Use HTML and PHP to achieve image display and management. The PHP code is as follows:
<?php $folder = "uploads/"; $files = glob($folder . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); foreach($files as $file) { echo "<a href='deleteImage.php?file=" . $file . "'><img src='" . $file . "' style='width:300px;height:200px;'></a>"; } ?>
Among them, "$folder" is the directory where uploaded images are saved, and "glob" is a PHP function used to obtain all files in the specified directory. Image file, the HTML code of the image displayed in the output loop, which contains a delete link, "deleteImage.php" is used to handle the operation of deleting the image.
2. Development of display management module
The display management module is another important module in CMS, which is used to create, edit, delete and display the multimedia content of the website. The following will introduce how to use PHP to develop a display management module.
Content creation and editing requires creating a form through which the user enters content and saves it to the database. Here is a simple code to create and edit a form:
<form action="saveContent.php" method="post"> <input type="text" name="title" placeholder="Title"> <textarea name="content" placeholder="Content"></textarea> <input type="submit" value="Save" name="submit"> </form>
After submitting the form, use PHP code to save the content to the database:
$title = $_POST["title"]; $content = $_POST["content"]; $sql = "INSERT INTO `content` (`title`, `content`) VALUES ('$title', '$content')";
Content display needs to obtain content from the database and output it to the page. The PHP code is as follows:
<?php $sql = "SELECT * FROM `content`"; $result = mysqli_query($conn, $sql); while($row = mysqli_fetch_array($result)) { echo "<h2>" . $row["title"] . "</h2>"; echo "<p>" . $row["content"] . "</p>"; } ?>
Among them, "mysqli_fetch_array" is used to loop the output content, and the output HTML code includes the title and content.
During the website display process, if you need to delete certain content, you can provide a deletion link and handle the deletion operation. The PHP code is as follows:
<?php $id = $_GET["id"]; $sql = "DELETE FROM `content` WHERE `id` = $id"; mysqli_query($conn, $sql); ?>
Among them, "$id" is the ID of the content to be deleted, and "DELETE" is the SQL statement. Execute this statement to delete the content with the specified ID. It should be noted that this method is prone to SQL injection risks, and security measures need to be added.
Summary:
In developing the picture and display management module in CMS, we need to master the core functions of PHP, including file upload, save, database operation, etc. At the same time, security factors need to be fully considered to filter and verify input to avoid risks such as SQL injection. Through studying this article, I believe readers can complete the development of image and display management modules in CMS.
The above is the detailed content of How to use PHP to develop image and display management modules in CMS. For more information, please follow other related articles on the PHP Chinese website!