Home > Article > Backend Development > How to use PHP developer city function: build image and video upload function
How to use PHP Developer City function: Build image and video upload function
With the rapid development of e-commerce, more and more people choose to shop online. Therefore, having a fully functional mall website has become crucial for businesses. When developing a city website, the upload function of pictures and videos is an essential component. This article will introduce how to use PHP to develop a city website and build image and video upload functions. The development steps and code examples are described step by step below.
Step one: Establish database and data table
Before starting, first create a MySQL database to store data on the mall website. Then, create a data table named "products" to store product details. The following is an example SQL statement:
CREATE DATABASE my_shop; USE my_shop; CREATE TABLE products ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, description TEXT, image VARCHAR(255), video VARCHAR(255) );
This data table contains the name, price, description, and file path of pictures and videos of the product.
Step 2: Create a web form
In order to upload image and video files, we need to create a form on the web page to enable users to select files and upload them to the server. The following is a sample HTML code:
<form action="upload.php" method="POST" enctype="multipart/form-data"> <label for="image">上传图片:</label> <input type="file" name="image" id="image"> <label for="video">上传视频:</label> <input type="file" name="video" id="video"> <input type="submit" value="提交"> </form>
This form contains two file upload input boxes, used to select image and video files respectively. The "action" attribute of the form points to a PHP file named "upload.php", which is used to handle file uploads.
Step 3: Process file upload
In the upload.php file, we need to write PHP code to process file upload and save the file to the server. The following is an example PHP code:
<?php $targetDirectory = "uploads/"; $imageFile = $targetDirectory . basename($_FILES["image"]["name"]); $videoFile = $targetDirectory . basename($_FILES["video"]["name"]); $uploadOk = true; $imageFileType = strtolower(pathinfo($imageFile, PATHINFO_EXTENSION)); $videoFileType = strtolower(pathinfo($videoFile, PATHINFO_EXTENSION)); // 检查文件类型 if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $videoFileType != "mp4" && $videoFileType != "mov" && $videoFileType != "avi") { echo "只允许上传JPG、JPEG、PNG、MP4、MOV和AVI格式的文件。"; $uploadOk = false; } // 检查文件大小 if ($_FILES["image"]["size"] > 5000000 || $_FILES["video"]["size"] > 50000000) { echo "文件大小超过限制。"; $uploadOk = false; } // 检查上传是否成功 if ($uploadOk) { if (move_uploaded_file($_FILES["image"]["tmp_name"], $imageFile) && move_uploaded_file($_FILES["video"]["tmp_name"], $videoFile)) { echo "文件上传成功。"; } else { echo "文件上传失败。"; } } ?>
The code first specifies an upload directory (for example, uploads/), and then obtains the uploaded file information through the $_FILES array. Next, it checks whether the file type and size meet the requirements. Finally, if the upload is successful, it will move the file to the specified directory and display the corresponding prompt message.
Step 4: Save the file path to the database
After the file is uploaded successfully, we need to save the file path to the database. Add the following code in the upload.php file:
// 获取提交的表单数据 $name = $_POST["name"]; $price = $_POST["price"]; $description = $_POST["description"]; // 连接到数据库 $conn = new mysqli("localhost", "username", "password", "my_shop"); // 插入数据到数据库 $sql = "INSERT INTO products (name, price, description, image, video) VALUES ('$name', $price, '$description', '$imageFile', '$videoFile')"; if ($conn->query($sql) === true) { echo "商品信息保存成功。"; } else { echo "商品信息保存失败: " . $conn->error; } // 关闭数据库连接 $conn->close();
This code first obtains the form data submitted through the POST method. It then uses mysqli to connect to the database and insert the form data and file paths into the "products" data table. Finally, it displays the corresponding prompt message.
Through the above steps, we have successfully built the image and video upload function of the mall website. When a user uploads a file, the file will be saved to the specified directory on the server, and the file path will be saved in the database so that product information and corresponding pictures and videos can be displayed on the website.
Summary
This article introduces how to use PHP Developer City website and builds the image and video upload function. By establishing a database, creating web forms, processing file uploads, and saving file paths to the database, we have implemented a complete shopping mall website function. I hope this article will be helpful to beginners using PHP Developer City website.
The above is the detailed content of How to use PHP developer city function: build image and video upload function. For more information, please follow other related articles on the PHP Chinese website!