简体   繁体   English

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

[英]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.

相关问题 pyautogui保存图像权限被拒绝 - Pyautogui saving image permission denied 使用pip安装flask时获得权限被拒绝错误 - Getting a permission denied error when installing flask with pip Flask 文件上传权限被拒绝 - Flask File Upload Permission Denied PermissionError: [Errno 13] 使用 pyinstaller 保存 txt 文件时权限被拒绝 - PermissionError: [Errno 13] Permission denied when saving a txt file with pyinstaller 尝试读取 excel 文件时如何修复 [Errno13] 权限被拒绝? - How to fix [Errno13] permission denied when trying to read excel file? 为什么在授予所有人对文件的完全访问权限后,我不断收到权限被拒绝错误? - Why do I keep getting the permission denied error after giving Everyone full access to the file? 为什么我在尝试打开 excel 文件时收到“PermissionError: [Errno 13] Permission denied”? - Why am I getting 'PermissionError: [Errno 13] Permission denied' when tried opening an excel file? 为什么我在激活 venv 时收到“权限被拒绝”? - Why am I getting "Permission denied" when activating a venv? 如何在Flask中向服务器请求数据(以图像形式)? - How do I request data (in form of image) to server in Flask? 如何在调整图像大小并将其保存到另一个文件夹时保留图像的文件名? - How do I keep the file name of an image when resizing it and saving it to another folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM