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

How to use php to add pictures

Mar 28, 2023 am 10:44 AM
php

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles