Allowed file types validation
📖 Scenario: You are building a simple Flask web app that lets users upload files. To keep the app safe and clean, you want to allow only certain file types like images.
🎯 Goal: Create a Flask app that accepts file uploads but only allows files with extensions .png, .jpg, and .jpeg. If a user tries to upload a file with a different extension, the app should reject it.
📋 What You'll Learn
Create a set called
ALLOWED_EXTENSIONS with the values 'png', 'jpg', and 'jpeg'.Write a function called
allowed_file(filename) that returns True if the file extension is in ALLOWED_EXTENSIONS, otherwise False.Use Flask's
request.files to get the uploaded file with the key 'file'.Check if the uploaded file is allowed using
allowed_file() before saving or processing.💡 Why This Matters
🌍 Real World
Web apps often need to accept user files but must restrict file types to avoid security risks and keep data clean.
💼 Career
Validating file uploads is a common task for web developers working with Flask or other web frameworks.
Progress0 / 4 steps