Home  >  Article  >  Backend Development  >  How to upload images through PHP

How to upload images through PHP

PHPz
PHPzOriginal
2023-04-04 10:40:311494browse

In website development, image uploading is a common requirement. Among them, it is a common practice to use PHP to upload images. This article will introduce how to upload images through PHP.

1. Preparation work

Before we start, we need to prepare some work:

(1) Web server (for example: Apache, NGINX, etc.)

(2) Install PHP (version required is 5.2 or above)

(3) Set the allowed size of uploaded files (modify in the php.ini configuration file)

(4) An HTML Form, used for users to upload files

Here we take the Apache server and PHP 5.5 as an example for introduction. The operations of other HTTP servers and PHP versions are similar.

2. Write an HTML form

Any upload operation must have an upload entry. Here we need to write an HTML form first and submit it to the server using the POST method.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP图片上传</title>
</head>
<body>
    <form action="upload.php" enctype="multipart/form-data" method="POST">
        请选择上传的文件:<br>
        <input type="file" name="file"><br><br>
        <input type="submit" value="上传"><br>
    </form>
</body>
</html>

Among them, the action attribute in the form points to the PHP file we want to write next, the enctype attribute must be set to "multipart/form-data", and the method attribute is POST.

3. Write PHP code

After the HTML form receives the files uploaded by the user, the data will be sent to the server. We need to receive the uploaded files and parameters through PHP on the server side. . Below is a simple PHP code that can receive uploaded images and save them to the server.

<?php
  header("Content-Type:text/html;charset=utf-8");

  $file = $_FILES[&#39;file&#39;];

  // 获取文件名
  $filename = $file[&#39;name&#39;];

  // 上传文件目录
  $upload_dir = &#39;uploads/&#39;;

  // 不存在该目录则创建
  if(!file_exists($upload_dir)) {
    mkdir($upload_dir);
  }

  // 把临时文件剪切到指定目录
  move_uploaded_file($file[&#39;tmp_name&#39;], $upload_dir.$filename)

  // 输出上传结果
  echo "上传成功!";
?>

In this code, first get the uploaded file. $_FILES is a PHP predefined variable. The name value of the file is the name value of the input tag we entered in the HTML form. Next, get the uploaded file name. , then define the upload directory, and determine whether the directory exists. If it does not exist, create it. Finally, cut the source file into the target directory and output a string indicating that the upload is successful.

It should be noted that before executing the move_uploaded_file method, we need to first determine whether the value of the 'error' field in the global variable $file is UPLOAD_ERR_OK. When this value is 0, it means that there is no error in the upload, otherwise it means that there is no error in the upload. wrong. We can also add some upload conditions to the code, such as upload file type, upload file size, etc., which can be implemented according to actual needs.

4. Run

to save the above code as an upload.php file and place it in the same directory as the HTML form code. Then you can test uploading images. Start the Apache service, open the browser and enter the address http://localhost/upload.html, select an image to upload, and you will see the successful upload message locally, and see the image we uploaded in the uploads directory. .

The above are the basic steps for uploading images in PHP. It is really simple to upload images. You only need some basic knowledge of HTML and PHP to quickly implement this function. Of course, in actual application scenarios, we also need to consider safety, reliability, etc., and pay more attention and testing.

The above is the detailed content of How to upload images through PHP. 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