簡體   English   中英

Flask 上傳權限被拒絕

[英]Flask Upload Permission Denied

我在 DigitalOcean 上托管帶有 Flask 和 Flask Uploads 的 Web 服務器,我試圖將文件上傳到靜態文件夾,但出現權限錯誤( OSError: [Errno 13] Permission denied: 'static' )。

我已經使用ls -la static檢查了該文件夾的 Linux 權限並返回:

drwxr-xr-x  7 root     root      4096 Feb 10 05:51 .
drwxr-xr-x  3 root     root      4096 Feb  2 07:04 ..
-rw-r--r--  1 root     root       262 Feb  2 06:36 dbconnect.py
-rw-r--r--  1 root     root       544 Feb  9 21:06 dbconnect.pyc
-rw-r--r--  1 root     root     41950 Feb 10 05:51 __init__.py
-rw-r--r--  1 www-data www-data 31080 Feb  2 08:14 __init__.pyc
drwxr-xr-x 10 root     root      4096 Jan 30 05:07 static
drwxr-xr-x  2 root     root      4096 Jan 30 05:07 templates
drwxr-xr-x  6 root     root      4096 Feb  9 20:45 venv

相同的代碼在我的本地 Windows 機器上運行良好,那么我還能嘗試什么來讓我將文件上傳到我的遠程服務器?

蟒蛇代碼:

import os, sys, os.path
from flask import Flask, render_template, flash, request, url_for, redirect, session, send_file, send_from_directory
from wtforms import Form, BooleanField, TextField, PasswordField, SelectField, RadioField, TextAreaField, DateField, DateTimeField, StringField, validators
from wtforms.widgets import TextArea
from wtforms.validators import DataRequired
from flask_wtf import FlaskForm, RecaptchaField
from flask_wtf.file import FileField, FileRequired, FileAllowed
from werkzeug.utils import secure_filename
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from passlib.hash import sha256_crypt
from MySQLdb import escape_string as thwart
from functools import wraps
from dbconnect import connection
app = Flask(__name__, static_folder='static')
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOADED_PHOTOS_DEST'] = 'static/user_info/prof_pic'
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app)  # set maximum file size, default is 16MB

class ProfilePictureForm(FlaskForm):
    prof_pic = FileField(validators=[FileAllowed(photos, u'Image only!')])

@app.route('/profile_picture_upload/', methods=['GET','POST'])
def profile_picture_upload():
    form = ProfilePictureForm()
    cid = str(session['clientcid'])
    first_name = session['first_name']
    #default_prof_pic = 'app/uploads/photos/static/user_info/prof_pic/default.jpg'
    #user_prof_pic = cid+'_'+first_name+'_'+'.png'
    if form.validate_on_submit():
        # Checks if the prof_pic is set yet. if set, then dont need to delete the old picture on the server
        if session['prof_pic'] != url_for('static', filename='user_info/prof_pic/default.jpg'):
            #need to delete or move the old prof_pic if it was set! Prevents users from adding too many pictures
            os.remove('static/user_info/prof_pic/'+cid+'_'+first_name+'.png')
            flash("You already have a file on the server!")
        filename = photos.save(form.prof_pic.data, name=cid+'_'+first_name+'.png')
        file_url = photos.url(filename) 
        session['prof_pic'] = file_url
        c, conn = connection()
        c.execute("UPDATE cpersonals SET prof_pic = %s WHERE cid = (%s)", (file_url, cid))

        conn.commit()
        c.close()
        conn.close()
    else:
        file_url = None

    return render_template('profile_picture_upload.html', form=form, file_url=file_url) 

有道理,但是如何將 UPLOADED_PHOTOS_DESTINATION 設置為一個級別? '../' 會工作嗎?

你有沒有嘗試改變:

app.config['UPLOADED_PHOTOS_DEST'] = 'static/user_info/prof_pic'

至:

app.config['UPLOADED_PHOTOS_DEST'] = 'app/static/user_info/prof_pic'

我遇到了類似的問題,這可能就是上面評論的意思。

試試 os.curdir+'/static/assets/img/'

看來我們需要在上傳目錄前面加上點才能使它工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM