Handling File Uploads With Flask - miguelgrinberg.com

PHOTO EMBED

Tue Sep 06 2022 13:10:37 GMT+0000 (Coordinated Universal Time)

Saved by @Rmin #python #flask #uploadfile

>>> from werkzeug.utils import secure_filename
>>> secure_filename('foo.jpg')
'foo.jpg'
>>> secure_filename('/some/path/foo.jpg')
'some_path_foo.jpg'
>>> secure_filename('../../../.bashrc')
'bashrc'
content_copyCOPY

In other cases it may be better to preserve the filenames provided by the client, so the filename must be sanitized first. For those cases Werkzeug provides the secure_filename() function. Let's see how this function works by running a few tests in a Python session:

https://blog.miguelgrinberg.com/post/handling-file-uploads-with-flask