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:
There are a few things to note here:
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.
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.
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:
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!