title: Flask - Uploading some image files causes 500 Internal Server Error... but not others?
body: <p>I'm making a website using flask for a CS50x Final Project, and I wanted my users to be able to add images to their profiles. So I set up the following basic application to test out the functionality:</p>
  <p><em><strong>application.py</strong></em></p>
  <pre><code>    import os
      from flask import *  
      app = Flask(__name__)  
      
      UPLOAD_FOLDER = 'uploads'
      app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER     
       
      @app.route('/')  
      def upload():  
          return render_template(&quot;file_upload_form.html&quot;)  
       
      @app.route('/upload', methods = ['POST'])  
      def upload():  
          if request.method == 'POST':  
              file = request.files['file']  
              file.save(os.path.join(app.config['UPLOAD_FOLDER'], &quot;test.jpg&quot;))
              return redirect(&quot;/&quot;)
  </code></pre>
  <p><em><strong>file_upload_form.html</strong></em></p>
  <pre><code>&lt;html&gt;  
  &lt;head&gt;  
      &lt;title&gt;Upload&lt;/title&gt;  
  &lt;/head&gt;  
  &lt;body&gt;  
      &lt;form action = &quot;/upload&quot; method = &quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;  
          &lt;input type=&quot;file&quot; name=&quot;file&quot; /&gt;  
          &lt;input type = &quot;submit&quot; value=&quot;Upload&quot;&gt;  
      &lt;/form&gt;  
  &lt;/body&gt;  
  &lt;/html&gt;
  </code></pre>
  <p>After testing this out, I found that for the majority of files I uploaded, it threw a <em><strong>500 Internal Server Error</strong></em> with no traceback or anything. Two images worked though - both JPEGs and both small files (less than 100KB on disk). I can't work out why some images would work and not others. Ideally, I'd like my website to consistently handle images.</p>
  <p>Any advice on where I'm going wrong?
  Thanks</p>
  <p>============== EDIT ==================</p>
  <p><em>redmicelles</em> kindly pointed out that I had two functions with the same name - sorry about that! The problem persists though.</p>
  <pre><code>import os
  from flask import *  
  app = Flask(__name__)  
  
  UPLOAD_FOLDER = 'uploads'
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER     
   
  @app.route('/')  
  def myindex():
      return render_template(&quot;file_upload_form.html&quot;)  
   
  @app.route('/upload', methods = ['POST'])  
  def upload():  
      if request.method == 'POST':  
          file = request.files['file']  
          file.save(os.path.join(app.config['UPLOAD_FOLDER'], &quot;test.jpg&quot;))
          return redirect(&quot;/&quot;)
          
  if __name__ == &quot;__main__&quot;:
      app.run(debug=True)
  </code></pre>
  <p>The terminal outputs the following:</p>
  <ul>
  <li>Serving Flask app &quot;application.py&quot; (lazy loading)</li>
  <li>Environment: development</li>
  <li>Debug mode: on</li>
  <li>Running on <a href="https://e08f1ea9-40e0-458c-8c38-6f7f5b5fb160-ide.cs50.xyz:8080/" rel="nofollow noreferrer">https://e08f1ea9-40e0-458c-8c38-6f7f5b5fb160-ide.cs50.xyz:8080/</a> (Press CTRL+C to quit)</li>
  <li>Restarting with stat</li>
  <li>Debugger is active!</li>
  <li>Debugger PIN: 113-512-723
  192.168.239.158 - - [06/Aug/2020 21:17:11] &quot;GET / HTTP/1.0&quot; 200 -</li>
  </ul>
  <p>So even with debug mode on, it doesn't give me any kind of traceback.</p>