Home > Backend Development > PHP Problem > How to use php to add pictures

How to use php to add pictures

PHPz
Release: 2023-03-28 11:04:59
Original
3757 people have browsed it

It is relatively simple to use PHP language to implement the function of adding pictures. The following will introduce a method of implementing the function of adding pictures in PHP based on the Web.

Step 1: Set up the form

In an HTML file, create a form form and set enctype to "multipart/form-data". This is used to tell the server that the form data will be transmitted in binary form.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="image">选择图片:</label>
  <input type="file" name="image" id="image">
  <input type="submit" name="submit" value="上传">
</form>
Copy after login

Step 2: Set the upload directory

Specify the directory where files can be uploaded and create the directory through a PHP script. Because the uploaded files need to be saved on the server side, this directory must be present.

$upload_dir = 'uploads/';

if (!file_exists($upload_dir)) {
  mkdir($upload_dir, 0777, true);
}
Copy after login

Step 3: Set the upload file name

If the user uploads a file with the same name, the name of the file uploaded later will often overwrite the previous file. To avoid this problem, we need to set a unique file name for the uploaded file.

$image_name = uniqid('img-', true) . '.' . strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
Copy after login

The above code uses PHP's uniqid() function to generate a unique file name. Then, we obtain the file extension through the pathinfo() function, and then convert it to lowercase letters through the strtolower() function to ensure the consistency of the file extension.

Step 4: Check the uploaded file type

Because users can upload any type of file, we need to ensure that the uploaded file is an image type to ensure that our web application Safety. We can check the uploaded file type by file extension or MIME type.

$allowed_types = array('jpg', 'jpeg', 'png', 'gif');
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $_FILES['image']['tmp_name']);

if (!in_array(strtolower(pathinfo($image_name, PATHINFO_EXTENSION)), $allowed_types) || !in_array($mime_type, array('image/jpeg', 'image/png', 'image/gif'))) {
  die('只支持上传JPG、PNG、GIF图片文件');
}
Copy after login

In the above code, we define a set of file types that are allowed to be uploaded, and then use the finfo library to determine the MIME type of the uploaded file. If the uploaded file type is not in the list of allowed types, the upload is terminated and an error message is output.

Step 5: Transfer the file to the server

We need to transfer the uploaded file from the client to the server. This can be achieved through PHP's move_uploaded_file() function:

$tmp_file = $_FILES['image']['tmp_name'];
move_uploaded_file($tmp_file, $upload_dir . $image_name);
Copy after login

In the above code, we obtain the temporary file of the file uploaded by the client and save the file to the upload directory, using the move_uploaded_file() function. After completing this step, the uploaded file has been successfully saved on the server.

Step 6: Display the image

After completing the upload, we need to display the image on the web page so that users can view the uploaded results.

echo '<img src="&#39; . $upload_dir . $image_name . &#39;" alt="">';
Copy after login

This code will output the uploaded image and return a successful result if there are no errors. Note that in actual application implementation, you need to use some means to handle errors that may occur during image uploading, such as file size exceeding the limit.

Summary

As a popular web development language, PHP has many advantages in implementing functions such as adding images. The above is a method to implement the function of adding pictures in PHP based on the Web. Readers can use this article as a reference method to implement the function of adding pictures by themselves, and on this basis, innovate and refine to achieve more refined effects.

The above is the detailed content of How to use php to add pictures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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