Saving upload in Flask only saves to project root











up vote
6
down vote

favorite
3












When I upload a new file, it saves to the application root folder, even though I specified a different UPLOAD_FOLDER. Why doesn't the config work?



views.py:



from flask import render_template
from flask import request, redirect, url_for,flash
from werkzeug.utils import secure_filename
from app import app
import os

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLD = '/Users/blabla/Desktop/kenetelli/htmlfi'
UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/')
def tmrf():
return render_template('main.html')


@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return 'file uploaded successfully'


__init__.py:



from flask import Flask

UPLOAD_FOLDER = ''
ALLOWED_EXTENSIONS = set('*.doc')

app = Flask(__name__)
app.config.from_object('config')
from app import views









share|improve this question




























    up vote
    6
    down vote

    favorite
    3












    When I upload a new file, it saves to the application root folder, even though I specified a different UPLOAD_FOLDER. Why doesn't the config work?



    views.py:



    from flask import render_template
    from flask import request, redirect, url_for,flash
    from werkzeug.utils import secure_filename
    from app import app
    import os

    APP_ROOT = os.path.dirname(os.path.abspath(__file__))
    UPLOAD_FOLD = '/Users/blabla/Desktop/kenetelli/htmlfi'
    UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

    @app.route('/')
    def tmrf():
    return render_template('main.html')


    @app.route('/uploader', methods=['GET', 'POST'])
    def upload_file():
    if request.method == 'POST':
    f = request.files['file']
    f.save(secure_filename(f.filename))
    return 'file uploaded successfully'


    __init__.py:



    from flask import Flask

    UPLOAD_FOLDER = ''
    ALLOWED_EXTENSIONS = set('*.doc')

    app = Flask(__name__)
    app.config.from_object('config')
    from app import views









    share|improve this question


























      up vote
      6
      down vote

      favorite
      3









      up vote
      6
      down vote

      favorite
      3






      3





      When I upload a new file, it saves to the application root folder, even though I specified a different UPLOAD_FOLDER. Why doesn't the config work?



      views.py:



      from flask import render_template
      from flask import request, redirect, url_for,flash
      from werkzeug.utils import secure_filename
      from app import app
      import os

      APP_ROOT = os.path.dirname(os.path.abspath(__file__))
      UPLOAD_FOLD = '/Users/blabla/Desktop/kenetelli/htmlfi'
      UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)
      app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

      @app.route('/')
      def tmrf():
      return render_template('main.html')


      @app.route('/uploader', methods=['GET', 'POST'])
      def upload_file():
      if request.method == 'POST':
      f = request.files['file']
      f.save(secure_filename(f.filename))
      return 'file uploaded successfully'


      __init__.py:



      from flask import Flask

      UPLOAD_FOLDER = ''
      ALLOWED_EXTENSIONS = set('*.doc')

      app = Flask(__name__)
      app.config.from_object('config')
      from app import views









      share|improve this question















      When I upload a new file, it saves to the application root folder, even though I specified a different UPLOAD_FOLDER. Why doesn't the config work?



      views.py:



      from flask import render_template
      from flask import request, redirect, url_for,flash
      from werkzeug.utils import secure_filename
      from app import app
      import os

      APP_ROOT = os.path.dirname(os.path.abspath(__file__))
      UPLOAD_FOLD = '/Users/blabla/Desktop/kenetelli/htmlfi'
      UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)
      app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

      @app.route('/')
      def tmrf():
      return render_template('main.html')


      @app.route('/uploader', methods=['GET', 'POST'])
      def upload_file():
      if request.method == 'POST':
      f = request.files['file']
      f.save(secure_filename(f.filename))
      return 'file uploaded successfully'


      __init__.py:



      from flask import Flask

      UPLOAD_FOLDER = ''
      ALLOWED_EXTENSIONS = set('*.doc')

      app = Flask(__name__)
      app.config.from_object('config')
      from app import views






      python flask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 23 '17 at 20:14









      davidism

      60.5k12150170




      60.5k12150170










      asked Feb 23 '17 at 19:47







      user7327263































          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          UPLOAD_FOLDER is not a configuration option recognized by Flask. f.save works relative to the current working directory, which is typically the project root during development.



          Join the secured filename to the upload folder, then save to that path.



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




          It's better to store local data in the instance folder, not the project root. Flask already knows where that is. Just make sure you create the instance directory first.



          import os
          from werkzeug.utils import secure_filename

          # create the folders when setting up your app
          os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True)

          # when saving the file
          f.save(os.path.join(app.instance_path, 'htmlfi', secure_filename(f.filename)))




          No matter where you decide to save it, you need to make sure the user running the application has write permission to that directory. If you get permission errors when running with mod_wsgi, for example, the user is commonly httpd or www-data. If you get a permission denied error, check that.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42424853%2fsaving-upload-in-flask-only-saves-to-project-root%23new-answer', 'question_page');
            }
            );

            Post as a guest































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            UPLOAD_FOLDER is not a configuration option recognized by Flask. f.save works relative to the current working directory, which is typically the project root during development.



            Join the secured filename to the upload folder, then save to that path.



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




            It's better to store local data in the instance folder, not the project root. Flask already knows where that is. Just make sure you create the instance directory first.



            import os
            from werkzeug.utils import secure_filename

            # create the folders when setting up your app
            os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True)

            # when saving the file
            f.save(os.path.join(app.instance_path, 'htmlfi', secure_filename(f.filename)))




            No matter where you decide to save it, you need to make sure the user running the application has write permission to that directory. If you get permission errors when running with mod_wsgi, for example, the user is commonly httpd or www-data. If you get a permission denied error, check that.






            share|improve this answer



























              up vote
              5
              down vote



              accepted










              UPLOAD_FOLDER is not a configuration option recognized by Flask. f.save works relative to the current working directory, which is typically the project root during development.



              Join the secured filename to the upload folder, then save to that path.



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




              It's better to store local data in the instance folder, not the project root. Flask already knows where that is. Just make sure you create the instance directory first.



              import os
              from werkzeug.utils import secure_filename

              # create the folders when setting up your app
              os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True)

              # when saving the file
              f.save(os.path.join(app.instance_path, 'htmlfi', secure_filename(f.filename)))




              No matter where you decide to save it, you need to make sure the user running the application has write permission to that directory. If you get permission errors when running with mod_wsgi, for example, the user is commonly httpd or www-data. If you get a permission denied error, check that.






              share|improve this answer

























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                UPLOAD_FOLDER is not a configuration option recognized by Flask. f.save works relative to the current working directory, which is typically the project root during development.



                Join the secured filename to the upload folder, then save to that path.



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




                It's better to store local data in the instance folder, not the project root. Flask already knows where that is. Just make sure you create the instance directory first.



                import os
                from werkzeug.utils import secure_filename

                # create the folders when setting up your app
                os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True)

                # when saving the file
                f.save(os.path.join(app.instance_path, 'htmlfi', secure_filename(f.filename)))




                No matter where you decide to save it, you need to make sure the user running the application has write permission to that directory. If you get permission errors when running with mod_wsgi, for example, the user is commonly httpd or www-data. If you get a permission denied error, check that.






                share|improve this answer














                UPLOAD_FOLDER is not a configuration option recognized by Flask. f.save works relative to the current working directory, which is typically the project root during development.



                Join the secured filename to the upload folder, then save to that path.



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




                It's better to store local data in the instance folder, not the project root. Flask already knows where that is. Just make sure you create the instance directory first.



                import os
                from werkzeug.utils import secure_filename

                # create the folders when setting up your app
                os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True)

                # when saving the file
                f.save(os.path.join(app.instance_path, 'htmlfi', secure_filename(f.filename)))




                No matter where you decide to save it, you need to make sure the user running the application has write permission to that directory. If you get permission errors when running with mod_wsgi, for example, the user is commonly httpd or www-data. If you get a permission denied error, check that.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 20 at 19:17

























                answered Feb 23 '17 at 20:17









                davidism

                60.5k12150170




                60.5k12150170






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42424853%2fsaving-upload-in-flask-only-saves-to-project-root%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini