Home  >  Article  >  Backend Development  >  How to print upload error message using PHP

How to print upload error message using PHP

PHPz
PHPzOriginal
2023-03-27 17:24:52542browse

PHP is a popular programming language that is widely used in web development and server-side application development. In web development, uploading files is a frequent requirement. However, due to various reasons, various errors may occur when uploading files, which makes development difficult.

This article will introduce how to use PHP to print upload error messages to help developers locate problems faster and solve upload file problems.

1. Common errors in uploading files

When uploading files, you often encounter the following errors:

  1. File size Limit exceeded

There is a configuration item named upload_max_filesize in PHP, which specifies the maximum size of uploaded files. If the uploaded file size exceeds this limit, a file size exceeded error will occur.

  1. Extensions are not allowed to be uploaded

When uploading files, there is usually a whitelist, and only allowed file extensions can be uploaded. If the uploaded file extension is not in the whitelist, an error not allowed to be uploaded will appear.

  1. The upload directory does not have permission

When uploading a file, you need to store the file in a directory on the server. However, in some cases, the upload directory does not have permission to be written, and then an error about upload directory not having permission will appear.

  1. Uploaded files are lost

When uploading files, sometimes the uploaded files are lost. This is usually due to a network outage, server crash, or other reasons.

2. Use PHP to print upload error messages

When an upload file error occurs, it is usually necessary to print the error message so that developers can locate and solve the problem. In order to print upload error information, you need to use some global variables and functions of PHP.

  1. Error message display methods

There are two error message display methods in PHP, namely "display on the page" and "record to the log file". During development, you can choose one of these ways to display error messages.

In the development environment, error information can be displayed on the page to facilitate debugging. In a production environment, error messages should be logged to a log file to avoid leaking sensitive information.

There is a function called error_reporting in PHP, which is used to set the error level. In a development environment, this can be set to E_ALL, and in a production environment, it can be set to E_ALL & ~E_NOTICE.

  1. Use global variable $_FILES to obtain uploaded file information

In PHP, use $_FILES global variable to obtain uploaded file information. To get information about uploaded files, you need to use the name attribute of this variable.

The following is a sample code:

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

In the PHP code, you can obtain uploaded file information through the $_FILES variable:

if ($_FILES["file"]["error"] > 0) {
  echo "错误:" . $_FILES["file"]["error"] . "<br>";
} else {
  echo "文件名:" . $_FILES["file"]["name"] . "<br>";
  echo "文件类型:" . $_FILES["file"]["type"] . "<br>";
  echo "文件大小:" . ($_FILES["file"]["size"] / 1024) . " KB<br>";
  echo "文件临时名称:" . $_FILES["file"]["tmp_name"] . "<br>";
}
  1. Print upload error message

If an error is encountered when uploading a file, the error message needs to be printed to facilitate developers to locate the problem. For common upload problems, you can refer to the following code:

switch ($_FILES["file"]["error"]) {
  case UPLOAD_ERR_INI_SIZE:
    echo "错误:上传文件大小超过了php.ini中指定的大小。";
    break;
  case UPLOAD_ERR_FORM_SIZE:
    echo "错误:上传文件大小超过了HTML表单中指定的大小。";
    break;
  case UPLOAD_ERR_PARTIAL:
    echo "错误:上传文件只有部分被上传。";
    break;
  case UPLOAD_ERR_NO_FILE:
    echo "错误:没有上传文件。";
    break;
  case UPLOAD_ERR_NO_TMP_DIR:
    echo "错误:找不到临时文件夹。";
    break;
  case UPLOAD_ERR_CANT_WRITE:
    echo "错误:无法将文件写入磁盘。";
    break;
  case UPLOAD_ERR_EXTENSION:
    echo "错误:文件上传受到扩展程序的影响。";
    break;
  default:
    echo "未知错误。";
    break;
}
  1. Upload files to the server

When processing uploaded files, you also need to save the uploaded files to the server in a directory. You can use the move_uploaded_file function to move files from the temporary directory to a specified directory.

//检查上传目录是否存在
if (!file_exists("uploads/")) {
  mkdir("uploads/");
}

//将上传的文件从临时目录移动到指定目录中
move_uploaded_file($_FILES["file"]["tmp_name"],
  "uploads/" . $_FILES["file"]["name"]);

Note: When saving the uploaded file, you should rename the file to avoid overwriting due to duplicate file names.

3. Summary

This article introduces file upload errors in PHP and how to print upload error messages. I hope it will be helpful to developers in solving file upload problems. When handling uploaded files, you need to pay attention to security issues, such as limiting uploaded file types, file sizes, etc. Through reasonable upload file processing techniques, developers can better handle file upload issues and improve the security and user experience of web applications.

The above is the detailed content of How to print upload error message using 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