Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask upload cannot find directory in server

Tags:

python

flask

I have problem with Flask upload in local machine code are working but when upload code to server using apache show error

IOError: [Errno 2] No such file or directory: u'app/static/avatars/KHUON.S.png'

Code :

ALLOWED_EXTENSIONS                  =   set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER']         =   'app/static/avatars'
app.config['MAX_CONTENT_LENGTH']    =   1 * 600 * 600

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/User/Profile', methods=['GET', 'POST'])
def upload_profile():

    if request.method == 'POST':

        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)

            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        else:

            flash("File extension not allow.")
            return redirect(url_for('upload_profile', upload='error'))

    return render_template("profile.html")
like image 912
Sovankiry Avatar asked Nov 22 '25 01:11

Sovankiry


1 Answers

Ok, in upload.py you could do something like

import os
absolute_path = os.path.abspath(UPLOAD_FOLDER+file_name)

os.path.abspath returns the absolute path from the given relative path.

like image 80
Mr Theavuth Avatar answered Nov 24 '25 14:11

Mr Theavuth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!