Home  >  Article  >  Backend Development  >  How to perform file upload processing in PHP7.0?

How to perform file upload processing in PHP7.0?

WBOY
WBOYOriginal
2023-05-26 08:21:111104browse

With the development of the Internet era, file upload plays a very important role in website development. When users need to upload their own avatars, photos or other files, the website needs to process the uploaded files. In PHP7.0, the way file upload is handled is slightly different from previous versions. This article will introduce how to perform file upload processing in PHP7.0.

1. HTML form for uploading files

In the HTML form, the 3525558f8f338d4ea90ebf22e5cde2bc tag is used for file upload. You need to set the enctype attribute to "multipart/form-data", and the submission method is the POST method, for example:

<form action="upload.php" method="POST" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="上传文件">
</form>

2. PHP processes uploaded files

In PHP, use the $_FILES array to manipulate uploaded files. When the user selects a file and submits the form, the $_FILES array will contain information about the uploaded file. For example:

$_FILES['file']['name'] // 文件名
$_FILES['file']['type'] // 文件类型
$_FILES['file']['size'] // 文件大小
$_FILES['file']['tmp_name'] // 临时文件名
$_FILES['file']['error'] // 错误代码

Among them, the temporary file name is used to read the content of the uploaded file after the upload is completed. Next, we'll cover how to use this information to upload files.

3. Upload files and save them to the server

You can use the move_uploaded_file() function to move the uploaded file from the temporary folder to the specified target folder. For example:

$target_dir = "uploads/"; // 目标文件夹
$target_file = $target_dir . basename($_FILES["file"]["name"]); // 目标文件路径
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "文件上传成功";
} else {
    echo "文件上传失败";
}

It should be noted that before receiving the uploaded file, you need to ensure that the target folder exists, otherwise the move_uploaded_file() function will fail.

4. Errors in handling uploaded files

Some errors may occur when uploading files, such as the uploaded file size exceeding the agreed size, upload failure, etc. We can use the error attribute in the $_FILES array to handle these errors.

switch ($_FILES["file"]["error"]) {
    case UPLOAD_ERR_OK: // 成功
        break;
    case UPLOAD_ERR_INI_SIZE: // 上传的文件超过了php.ini中的最大约束大小
    case UPLOAD_ERR_FORM_SIZE: // 上传的文件超过了HTML表单中的最大约束大小
        echo "文件太大";
        break;
    case UPLOAD_ERR_PARTIAL: // 文件只有部分被上传
        echo "文件上传失败";
        break;
    case UPLOAD_ERR_NO_FILE: // 没有文件被上传
        echo "请选择文件";
        break;
    case UPLOAD_ERR_NO_TMP_DIR: // 服务器未配置临时目录
    case UPLOAD_ERR_CANT_WRITE: // 无法将文件写入磁盘
    case UPLOAD_ERR_EXTENSION: // 文件不能通过PHP扩展上传
        echo "文件上传失败";
        break;
}

5. Verify the type and size of uploaded files

In order to avoid illegal uploaded files, we also need to verify the type and size of uploaded files. For example, if only image type files are allowed to be uploaded, you can use the getimagesize() function to determine the file type.

$allowed_types = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$type = exif_imagetype($_FILES["file"]["tmp_name"]);
if (!in_array($type, $allowed_types)) {
    echo "请选择正确的图像类型";
}

At the same time, the file size also needs to be verified. You can use PHP's filesize() function to obtain the file size, for example:

$max_size = 2 * 1024 * 1024; // 允许上传最大文件大小为2MB
if (filesize($_FILES["file"]["tmp_name"]) > $max_size) {
    echo "文件太大";
}

6. Summary

In PHP7.0 , file upload processing becomes more secure and simpler. You can obtain uploaded file information through the $_FILES array, use the move_uploaded_file() function to save the file to the server, and verify the uploaded file type and size to ensure that the uploaded file is safe and reliable.

The above is the detailed content of How to perform file upload processing in PHP7.0?. 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