Home > Article > Backend Development > How to limit file upload types in php
1. Create a new form.
Open the PHP editor and create a new form for uploading files. In the form, link the submitted address to upload_file1.php.
#2. Set the character set of the php file.
Then create a new php file that receives the uploaded file. First, we set the character set of the php to "utf-8".
header("Content-type:text/html;charset=utf-8");
3. Set the file types allowed to be uploaded.
Then we set the uploaded file type to "'jpg','png','gif','jpeg'". $allowexts=['jpg','png','gif','jpeg'];
4. Extract the uploaded file name suffix.
Extract the suffix of the uploaded file through the explode and end functions.
$temp=explode('.', $_FILES['file']['name']); $extend=end($temp);
5. Determine the file type.
For the uploaded file, determine its file type and determine whether its suffix is the file type we allow to upload.
6. Return illegal files.
If the type of the uploaded file does not meet our requirements, the prompt message "invalid file" will be output.
7. Carry out testing.
After the code is written, we can test it in the browser. Enter the address, press Enter to access, and select a file to upload for testing.
Recommended online video tutorial: php video tutorial
The above is the detailed content of How to limit file upload types in php. For more information, please follow other related articles on the PHP Chinese website!