We are having problem having Flask save image files on our server. We’ve made sure that the directory we’re trying to write to has write permission. We used chmod images 777. But we are getting “Permission Denied”.
I did "chmod 777 images" directory ls -l result is drwxrwxrwx 2 someuser someuser 4096 Jul 16 2021 images
Result of form with file submission: Same response: Permission Denied
Here are the relevant fragments of code we have:
<form action="/someurl" method="post" enctype="multipart/form-data" id="workform">
<input type="file" name="photobefore” />
<input type="file" name="photoafter" />
<input type=“submit” />
</form>
SERVER:
@main_blueprint.route(‘/someurl’, methods=['GET', 'POST'])
def workperformed():
message = “submitted”
before = ""
after = ""
try:
if len(request.files) > 0:
for name in request.files:
file = request.files[name]
if name == 'photobefore':
before = file.filename
if name == 'photoafter':
after = file.filename
if not file.filename == '':
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
except Exception as e:
message = "dir:" os.getcwd() " error:" str(e)
return message
Result: dir:/ error:[Errno 13] Permission denied: '/home/someuser/public_html/mydefensiblespace.org/html/templates/static/images/20190721_184017_HDR.jpg'
CodePudding user response:
The web server (assuming Apache or Nginx on Ubuntu) needs to be the owner of the folder where you are saving images
chown -R www-data:www-data /path/to/folder
I believe Nginx also needs the folder to be executable
chmod x /path/to/folder
Also since your path to folder is outside of /var/www
you will need a directive in your virtual host
<VirtualHost *:80>
ServerName domain.com
# ...
<Directory /path/to/folder>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>