Home >Database >Mysql Tutorial >How to Restrict File Upload Types and Size in PHP?
Restricting File Upload Types in PHP
You're facing issues with restricting file upload types to PDF, DOC, or DOCX and limiting file size to less than 400 KB. Your provided code attempts to validate file extensions and sizes; however, it has some flaws.
To resolve these issues, you can use the following code:
<code class="php">function allowed_file() { // Define allowed MIME types $allowed_types = array('application/doc', 'application/docx', 'application/pdf'); // Validate uploaded files if (in_array($_FILES['resume']['type'], $allowed_types) && in_array($_FILES['reference']['type'], $allowed_types)) { // Check file sizes if ($_FILES["resume"]["size"] < 400000 && $_FILES["reference"]["size"] < 400000) { // Files allowed for upload } else { // File size exceeded limit } } else { // Invalid file type } }</code>
Explanation:
The above is the detailed content of How to Restrict File Upload Types and Size in PHP?. For more information, please follow other related articles on the PHP Chinese website!