Concept Flow - Secure filename handling
User uploads file
Extract filename
Sanitize filename
Save file with safe name
Return success or error
This flow shows how Flask handles uploaded filenames safely by cleaning them before saving.
from flask import Flask, request from werkzeug.utils import secure_filename filename = secure_filename(request.files['file'].filename)
| Step | Action | Input Filename | Sanitized Filename | Reason |
|---|---|---|---|---|
| 1 | Receive file upload | my photo.png | User uploads a file with spaces | |
| 2 | Call secure_filename() | my photo.png | my_photo.png | Spaces replaced with underscores |
| 3 | Check for unsafe chars | my_photo.png | my_photo.png | No unsafe chars found |
| 4 | Return sanitized filename | my_photo.png | Ready to save safely | |
| 5 | Save file | my_photo.png | File saved with safe name | |
| 6 | End | Process complete |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| filename | my photo.png | my_photo.png | my_photo.png | my_photo.png |
Use werkzeug.utils.secure_filename(filename) to clean filenames. It replaces spaces with underscores and removes unsafe characters. Prevents path traversal attacks by stripping dangerous parts. Always sanitize filenames before saving uploaded files. Ensures files save safely in your server folder.
secure_filename in Flask when handling file uploads?secure_filenamesecure_filename in a Flask app?secure_filenamesecure_filename is provided by werkzeug.utils, not directly by Flask.from werkzeug.utils import secure_filename. Other options are either incorrect syntax or wrong modules.from werkzeug.utils import secure_filename
filename = secure_filename('../../etc/passwd')secure_filename handles path traversalfrom werkzeug.utils import secure_filename
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
filename = secure_filename(file.filename)
file.save('/uploads/' + filename)
return 'File saved'request.files but does not import request from Flask, causing a NameError.secure_filename, the method POST is correct, and folder existence is not checked but not an error here.file is from request.files?
from werkzeug.utils import secure_filename
import os
UPLOAD_FOLDER = os.path.join(os.getcwd(), 'uploads')
# Which snippet below is correct?
A) filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename))
B) filename = file.filename
file.save(os.path.join(UPLOAD_FOLDER, filename))
C) filename = secure_filename(file.filename)
file.save('/uploads/' + filename)
D) filename = secure_filename(file.filename)
file.save(os.path.join('/uploads', filename))secure_filename correctly clean the filename (eliminate the snippet using raw file.filename).os.path.join(UPLOAD_FOLDER, filename) with the defined UPLOAD_FOLDER that points inside the project 'uploads' folder. The remaining snippets use hardcoded paths like '/uploads/' + filename or os.path.join('/uploads', filename), which may not save inside the project folder.Pass the test and this pattern moves unknown