Home > Backend Development > PHP Tutorial > PHP function file upload function

PHP function file upload function

WBOY
Release: 2023-05-18 12:34:01
Original
1971 people have browsed it

PHP, as a popular server-side programming language, is widely used in web application development. The file upload function is also one of the commonly used functions in PHP. The file upload function can be easily implemented using PHP's file upload function.

Introduction to the PHP file upload function

The PHP file upload function is mainly used to upload files transferred by the client to the server. In PHP, there are two core functions that can implement the file upload function, namely the move_uploaded_file and is_uploaded_file functions.

move_uploaded_file function

The move_uploaded_file function is mainly used to move the files uploaded by the client to the specified directory. Its basic syntax is as follows:

move_uploaded_file($_FILES['file']['tmp_name'], $target_dir . $file_name);
Copy after login

Among them, the first parameter It is the source file path of the moved file, which is $_FILES'file'; the second parameter is the target file path, which is the directory where the file is located after the move, and also includes the file name.

is_uploaded_file function

The is_uploaded_file function is mainly used to determine whether it is an uploaded file. The basic syntax is as follows:

is_uploaded_file($_FILES['file']['tmp_name'])
Copy after login

Among them, $_FILES is the global variable of the uploaded file, and 'tmp_name' is the temporary storage location of the uploaded file. The is_uploaded_file function returns a Boolean value, true indicates that the file was uploaded via HTTP POST, false indicates that the file may not have been uploaded via HTTP POST.

PHP file upload process

Next, we will introduce in detail how to use PHP to implement the file upload function. Generally speaking, file upload requires the following steps:

  1. Create a file upload form, including form elements, form attributes, and form submission methods.
  2. Receive files uploaded by the client and generally use $_FILES to process them.
  3. Verify the files uploaded by the client, including file size, file type, file name, etc.
  4. Regarding the problem of saving uploaded files, you can move the files uploaded by the client to the specified directory through the move_uploaded_file function.
  5. Finally, you can give the user a prompt that the upload is successful or jump to the next page.

Sample code:

The following code is an example of a simple file upload function:

<!DOCTYPE html>
<html>
<head>
    <title>文件上传示例</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" name="submit" value="上传"/>
</form>
<?php
if(isset($_POST['submit'])){//判断是否有提交
    if(isset($_FILES['file'])){//确保文件存在
        $target_dir = "uploads/";//指定上传的目录
        $file_name = basename($_FILES['file']['name']);//获取上传文件名
        $target_file = $target_dir . $file_name;//指定上传文件路径
        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_file)){//上传成功
            echo "上传成功!文件保存于:".$target_file;
        }else{//上传失败
            echo "上传失败!";
        }
    }else{//文件不存在
        echo "请选择要上传的文件!";
    }
}
?>
</body>
</html>
Copy after login

In the above code, a submit button and a file selection are used The box implements a simple upload function. In PHP, file upload mainly obtains the uploaded file information through the $_FILES array variable. If the file is uploaded successfully, it can be moved to the set directory for storage through the move_uploaded_file() function.

Conclusion

Through this article, we introduced the commonly used file upload functions in PHP and the implementation process of file upload. In the process of implementing file upload, you need to carefully consider the user's upload requirements and possible problems, and also pay attention to preventing upload security issues. Only after completing these preparations can a truly efficient and stable file upload function be achieved.

The above is the detailed content of PHP function file upload function. 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