A. secure_filename should be called on file.filename, not file
B. file.save() requires a file object, not a path
C. os.path.join cannot join strings
D. request.files['file'] returns a string, not a file object
Solution
Step 1: Check usage of secure_filename
secure_filename expects a string filename, but file is a file object, so file.filename should be passed.
Step 2: Confirm correct file saving steps
After getting a safe filename, join path and call file.save(path) correctly.
Final Answer:
secure_filename should be called on file.filename, not file -> Option A
Quick Check:
Pass filename string to secure_filename [OK]
Hint: Use secure_filename(file.filename), not file itself [OK]
Common Mistakes:
Passing file object to secure_filename
Confusing file.save argument type
Misusing os.path.join with non-string
Assuming request.files returns string
5. You want to allow users to upload multiple images at once and save only PNG files securely. What issue does this Flask code snippet have?
files = request.files.getlist('images')
for file in files:
if file and file.filename.endswith('.png'):
filename = secure_filename(file.filename)
file.save(os.path.join('uploads', filename))
hard
A. file.filename.endswith('.png') is case-sensitive and may miss '.PNG'
B. getlist() returns a single file, so loop will fail
C. secure_filename should be called after saving the file
D. This code correctly filters PNG files and saves them securely
Solution
Step 1: Understand getlist() usage
request.files.getlist('images') correctly returns a list of uploaded files for the field 'images'.
Step 2: Check file extension filtering
Using file.filename.endswith('.png') is case-sensitive and will miss files with uppercase extensions like '.PNG'.
Step 3: Confirm secure_filename usage
secure_filename is correctly called before saving the file.
Final Answer:
file.filename.endswith('.png') is case-sensitive and may miss '.PNG' -> Option A
Quick Check:
Check file extension case-insensitively [OK]
Hint: Check file extensions case-insensitively for uploads [OK]
Common Mistakes:
Assuming getlist returns one file
Calling secure_filename after saving
Ignoring uppercase file extensions
Not checking if file is None before saving
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.