Home > Backend Development > PHP Tutorial > PHP form processing example uploading files

PHP form processing example uploading files

WBOY
Release: 2023-06-20 17:38:01
Original
1754 people have browsed it

PHP form processing example of uploading files

In website development, we often need to provide users with the function of uploading files, such as avatars, pictures, documents, etc. As a back-end language, PHP can easily implement the file upload function. In this article, we will implement the file upload function based on PHP.

1. Pre-requisite knowledge

Before starting, there are some basic PHP knowledge that must be mastered:

  1. Collect form data: PHP can pass $_POST , $_GET and other global variables to obtain the form data.
  2. File upload: PHP file upload is implemented through the $_FILES global variable, which contains information about the uploaded file.
  3. File operation: PHP can operate files through file operation functions. For example, the move_uploaded_file() function can move uploaded files to a specified directory.

2. File upload example

Below, let us learn how to use PHP to process form data and file upload through a file upload example.

  1. HTML file

Let’s first create a simple HTML file, which contains a form for uploading files:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文件上传测试</title>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
Copy after login

In the form above , we set the form's submission address to "upload.php", and also set the form's enctype attribute to "multipart/form-data", which is necessary because we want to upload files instead of regular text data.

  1. PHP file

Next, we need to write the PHP file "upload.php" that handles file upload. The code is as follows:

<?php
if (isset($_FILES['file'])) {
    $file = $_FILES['file'];

    // 文件存储路径
    $target = 'uploads/' . $file['name'];

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

above In the code, we first determine whether there is an uploaded file, then obtain the relevant information of the file, and move the file to the specified directory through the move_uploaded_file() function. If the upload is successful, "File Upload Successful" is output, otherwise "File Upload Failed" is output.

  1. File storage directory

Finally, we need to create a directory on the server to store the uploaded files. We can specify the storage directory in the PHP file, as above "uploads/" in the code. Of course, we can also upload to different directories by specifying different action attribute values ​​​​in the HTML file.

3. Summary

Through the examples in this article, we can learn how to use PHP to process form data and file upload functions. In actual development, you need to pay attention to the security of file uploads, such as file type, file size and other restrictions, which can be achieved through verification functions and configuration files. I hope this example can bring you some help and inspiration in development.

The above is the detailed content of PHP form processing example uploading files. For more information, please follow other related articles on the PHP Chinese website!

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