I am hosting a web server with Flask and Flask Uploads on DigitalOcean, I am attempting to upload a file to the static folder, but am getting a permissions error ( OSError: [Errno 13] Permission denied: 'static'
).
I have checked the Linux permissions to that folder with ls -la static
and get back:
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
The same code I have works fine on my local Windows machine, so what else could I try that will allow me to upload files to my remote server?
Python code:
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)
Makes sense, but how do I set the UPLOADED_PHOTOS_DESTINATION to one up a level? Would ‘../‘ work?
Did you try to change:
app.config['UPLOADED_PHOTOS_DEST'] = 'static/user_info/prof_pic'
To:
app.config['UPLOADED_PHOTOS_DEST'] = 'app/static/user_info/prof_pic'
I ran into a similar problem and that may be what was meant by the comment above.