[英]How do I fix problem with getting "permission denied" when Flask server is saving an image file
We are having problem having Flask save image files on our server.我们在将 Flask 图像文件保存在我们的服务器上时遇到问题。 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”.我们使用 chmod images 777。但我们得到“权限被拒绝”。
I did "chmod 777 images" directory ls -l result is drwxrwxrwx 2 someuser someuser 4096 Jul 16 2021 images我做了“chmod 777 images”目录 ls -l 结果是 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'结果:dir:/error:[Errno 13] Permission denied: '/home/someuser/public_html/mydefensiblespace.org/html/templates/static/images/20190721_184017_HDR.jpg'
The web server (assuming Apache or Nginx on Ubuntu) needs to be the owner of the folder where you are saving images web 服务器(假设 Apache 或 Nginx 在 Ubuntu 上)需要是您保存图像的文件夹的所有者
chown -R www-data:www-data /path/to/folder
I believe Nginx also needs the folder to be executable我相信 Nginx 也需要该文件夹才能执行
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此外,由于您的文件夹路径在/var/www
之外,因此您需要在虚拟主机中使用一个指令
<VirtualHost *:80>
ServerName domain.com
# ...
<Directory /path/to/folder>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.