繁体   English   中英

当 Flask 服务器正在保存图像文件时,如何解决出现“权限被拒绝”的问题

[英]How do I fix problem with getting "permission denied" when Flask server is saving an image file

我们在将 Flask 图像文件保存在我们的服务器上时遇到问题。 我们已确保我们尝试写入的目录具有写入权限。 我们使用 chmod images 777。但我们得到“权限被拒绝”。

我做了“chmod 777 images”目录 ls -l 结果是 drwxrwxrwx 2 someuser someuser 4096 Jul 16 2021 images

提交文件的表单结果:相同的响应:权限被拒绝

以下是我们拥有的相关代码片段:

<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>

服务器:

@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

结果:dir:/error:[Errno 13] Permission denied: '/home/someuser/public_html/mydefensiblespace.org/html/templates/static/images/20190721_184017_HDR.jpg'

web 服务器(假设 Apache 或 Nginx 在 Ubuntu 上)需要是您保存图像的文件夹的所有者

chown -R www-data:www-data /path/to/folder

我相信 Nginx 也需要该文件夹才能执行

chmod +x /path/to/folder 

此外,由于您的文件夹路径在/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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM