Home  >  Article  >  Backend Development  >  Submit files in PHP

Submit files in PHP

WBOY
WBOYOriginal
2023-05-24 08:51:221027browse

PHP is a server-side scripting language that can be used to process form data, generate dynamic pages, etc. One common application scenario is uploading files through forms. This article will introduce the file upload function in PHP, including the processing of upload forms, storage of uploaded files, limiting the type and size of uploaded files, etc.

1. Processing of upload forms

In HTML, you can create a form through the form tag, and use the type="file" attribute in the input tag to allow uploading files. In PHP, you can use the $_FILES global variable to obtain uploaded file information.

Suppose our form contains an input tag for uploading files and a submit button:

11b6fd11255bf39aea2e39bb793902ba
e1afba264bef770f92ac9a2c4a571c26
911f4ad4d66a36c39659a323a3785a5a

There are a few things to note here:

  1. The action attribute specifies the address for form submission, which is the processing script for uploading files. Here we use upload.php file.
  2. The method attribute specifies the form submission method, here the post method is used.
  3. The enctype attribute specifies the encoding method of form data. We need to use multipart/form-data to allow uploading files.

Next, handle the file upload request in the upload.php file.

2. Storage of uploaded files

First you need to determine whether the uploaded file exists. If it exists, you can obtain the relevant information of the uploaded file through the $_FILES array:

if( isset($_FILES["fileToUpload"])) {

$file_name = $_FILES["fileToUpload"]["name"];
$file_tmp = $_FILES["fileToUpload"]["tmp_name"];
$file_size = $_FILES["fileToUpload"]["size"];

}

Among them, the file name can be obtained through the $name attribute, the file size can be obtained through the $size attribute, and the temporary path of the file Can be obtained through the $tmp_name attribute.

Next, you need to move the uploaded file to the target folder. Here use the move_uploaded_file() function:

$target_dir = "uploads/";
$target_file = $target_dir. basename($file_name);
if(move_uploaded_file($file_tmp, $target_file)) {

echo "The file ". basename($file_name). " has been uploaded.";

} else{

echo "Sorry, there was an error uploading your file.";

}

In the above code, $target_dir is the storage directory of the uploaded file, where the basename() function is used to obtain the file name. Moving files uses the move_uploaded_file() function, where the first parameter is the temporary path of the file, and the second parameter is the target path. If the file is moved successfully, a successful upload message is returned to the user, otherwise a failed upload message is returned.

3. Limit the type and size of uploaded files

In order to ensure the security and performance of the uploading system, we need to limit uploaded files. This includes both file type and file size.

  1. File type restrictions

We use PHP's in_array() function to determine whether the type of uploaded file is legal. Specifically, you need to define an array of file types that are allowed to be uploaded, and then use the in_array() function to determine whether the uploaded file type is in the array of file types that are allowed to be uploaded.

$allowed_types = array('jpg', 'jpeg', 'png', 'gif');
$file_type = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
if( in_array($file_type, $allowed_types)){

// 合法文件类型

} else{

echo "Sorry, only JPG, JPEG, PNG, GIF files are allowed.";

}

In the above code, the pathinfo() function is used to obtain the file suffix name , the strtolower() function converts it to lowercase characters, and the in_array() function determines whether it is in the array of file types that are allowed to be uploaded.

  1. File size limit

We use PHP's file upload configuration to limit the size of uploaded files, which can be set in the php.ini file, for example:

post_max_size=8M
upload_max_filesize=2M

The post_max_size here represents the maximum length of POST data, and upload_max_filesize represents the maximum size of the uploaded file. It should be noted that if the file size does not meet the limit, the $_FILES array will not contain information about the uploaded file.

Summary:

Implementing the file upload function in PHP requires the following steps:

  1. Create a form containing the upload file input tag and submit button.
  2. In the processing script for uploading files, use the $_FILES global variable to obtain the uploaded file information.
  3. Move the uploaded files to the target folder.
  4. Enforce security and performance restrictions on uploaded files, including file type and file size.

Implementing the file upload function through the above method can provide our web applications with richer functions and better user experience.

The above is the detailed content of Submit files in 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