Home > Backend Development > PHP Problem > How to upload pictures in php

How to upload pictures in php

PHPz
Release: 2023-04-19 09:39:17
Original
4850 people have browsed it

With the continuous development and popularization of the Internet, pictures play an increasingly important role in the Internet, and in website development, uploading pictures is also a very common operation. As a powerful server-side language, PHP is excellent at uploading images. So, how to upload images in PHP? Let’s introduce it in detail below.

1. Front-end code

To upload images on the front-end page, you need to use the control whose type attribute is file in the input tag. The code is as follows:

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
</form>
Copy after login
Copy after login

Among them, the action attribute of the form tag points to upload.php, the handler for uploading images, the method attribute specifies the submission method of the form as post, and the enctype attribute sets the encoding method of the form.

The type attribute in the input tag is file, and the name attribute specifies the name of the uploaded file.

The submit attribute in the input tag is the submit button in the form.

It should be noted that the enctype attribute in the form tag must be set to "multipart/form-data", otherwise the file cannot be uploaded.

2. Back-end code

To implement the function of uploading images in PHP, you need to obtain the uploaded files through the PHP built-in variable $_FILES. The code is as follows:

<?php
    // 判断是否有文件上传
    if(empty($_FILES[&#39;file&#39;][&#39;name&#39;])) {
        echo "请选择要上传的文件";
        exit;
    }

    // 打印上传的文件信息
    echo "文件名:" . $_FILES[&#39;file&#39;][&#39;name&#39;] . "<br>";
    echo "文件大小:" . $_FILES['file']['size'] . "<br>";
    echo "文件类型:" . $_FILES['file']['type'] . "<br>";

    // 上传文件的目录
    $upload_dir = "uploads/";
    // 上传文件的全名
    $upload_file = $upload_dir . $_FILES['file']['name'];

    // 将上传的文件从临时目录移动到指定目录
    if(move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
        echo "文件上传成功";
    } else {
        echo "文件上传失败";
    }
?>
Copy after login
Copy after login

Lines 1-3 determine whether there is a file uploaded, and if not, a prompt message is output.

Lines 5-7 print the uploaded file information, including file name, file size and file type.

Lines 9-10 are to set the directory for uploading files and the full name of the uploaded file.

Lines 12-16 move the uploaded file from the temporary directory to the specified directory through the move_uploaded_file function. The move_uploaded_file function has two parameters. The first parameter is the location of the uploaded file in the temporary directory, and the second parameter is the location of the uploaded file in the storage directory. Returns true if the move is successful, false otherwise.

3. Complete code

Front-end code:

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
</form>
Copy after login
Copy after login

Back-end code:

<?php
    // 判断是否有文件上传
    if(empty($_FILES[&#39;file&#39;][&#39;name&#39;])) {
        echo "请选择要上传的文件";
        exit;
    }

    // 打印上传的文件信息
    echo "文件名:" . $_FILES[&#39;file&#39;][&#39;name&#39;] . "<br>";
    echo "文件大小:" . $_FILES['file']['size'] . "<br>";
    echo "文件类型:" . $_FILES['file']['type'] . "<br>";

    // 上传文件的目录
    $upload_dir = "uploads/";
    // 上传文件的全名
    $upload_file = $upload_dir . $_FILES['file']['name'];

    // 将上传的文件从临时目录移动到指定目录
    if(move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
        echo "文件上传成功";
    } else {
        echo "文件上传失败";
    }
?>
Copy after login
Copy after login

4. Security considerations

When implementing upload When using images, you need to pay attention to some security issues. Uploading files may cause problems such as code injection and file overwriting, so some security processing is required. The following are some common security measures:

  1. Verify uploaded files, verify file type, size, etc. to prevent illegal files from being uploaded.
  2. Rename the uploaded file to prevent the uploaded file from having the same name as an existing file, causing the file to be overwritten.
  3. Store uploaded files in a non-Web-accessible directory to prevent malicious access.

In short, continuous optimization and upgrading are required during the development process, and security is placed in an important position to ensure the reliability of the code.

Summary:

The method of uploading images in PHP is relatively simple. You only need to use the input tag of file type in HTML, then obtain the uploaded file through the $_FILES variable, and finally save the file from the temporary directory. Just move it to the specified directory. In order to ensure security, the uploaded files need to be verified, renamed, and the security settings of the storage directory etc. Uploading pictures is a common operation in website development, and it is especially widely used in websites such as social networking and e-commerce platforms.

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

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