title: IOError: Errno 13 Permission denied for specific files
body: <p>I'm running a flask API which can be used to upload jpg files.
  It's been working fine for about a year, but today, out of the blue, uploaded files are rejected for a specific user of the API.
  There's actually not any difference in the treatment of the request from one user to another, but the same file gets rejected for user A and accepted for user B, and user A never had this problem before. It's driving me crazy !</p>
  
  <p>Here's the code in the flask API :</p>
  
  <pre><code>from werkzeug.utils import secure_filename
  
  ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  
  def allowed_file(filename):
      return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  
  
  @app.route('/uploadFile/', methods=['POST'])
  @auth.login_required
  def newImage():
      if request.method == 'POST':
          if request.files:
              file = request.files['file']
              if allowed_file(file.filename):
                  ImageId = request.args.get('ImageId')
                  newImage = DB_Image()
                  newImage.ObjectId = ImageId
  
                  filename = secure_filename(file.filename)
                  pictureName, fileExtension = os.path.splitext(filename)
                  fullFileNameToBeSaved = str(newImage.ObjectId) + fileExtension
                  imagesPath = os.path.join(app.static_folder, 'images')
  
                  file.save(os.path.join(imagesPath, fullFileNameToBeSaved))
                  (...)
  </code></pre>
  
  <p>Everything works fine until the file.save, where I get the following error when user A tries to upload a file:</p>
  
  <pre><code>mod_wsgi (pid=14170): Exception occurred processing WSGI script '/var/www/myApi/wsgi/myapivenv.wsgi'.
  Traceback (most recent call last):
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
       return self.wsgi_app(environ, start_response)
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
       response = self.make_response(self.handle_exception(e))
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
       reraise(exc_type, exc_value, tb)
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
       response = self.full_dispatch_request()
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
       rv = self.handle_user_exception(e)
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
       reraise(exc_type, exc_value, tb)
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
       rv = self.dispatch_request()
     File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
       return self.view_functions[rule.endpoint](**req.view_args)
     File "/usr/local/lib/python2.7/dist-packages/flask_httpauth.py", line 61, in decorated
       return f(*args, **kwargs)
     File "/var/www/myApi/code/ImageUpload.py", line 227, in newImage
       file.save(os.path.join(imagesPath, fullFileNameToBeSaved))
     File "/usr/local/lib/python2.7/dist-packages/werkzeug/datastructures.py", line 2576, in save
       dst = open(dst, 'wb')
   IOError: [Errno 13] Permission denied: '/var/www/path_to_upload_folder/files/images/2a6d85db-7ef0-40e3-8031-b7ed490bc512.jpg'
  </code></pre>
  
  <p>The permissions were set when I installed the API, for the unix user created specifically to run the API :</p>
  
  <pre><code>$ sudo usermod -a -G myApi $USER
  $ sudo chown -R $USER:myApi var/www/path_to_upload_folder/ path_to_upload_folder/files/images
  $ sudo chmod -R g+w var/www/path_to_upload_folder/ path_to_upload_folder/files/images
  </code></pre>
  
  <p>But the users of the API are managed in a database, they have nothing to do with unix users, so I highly doubt the problem is from there.</p>
  
  <p>What could cause an error like this on some specific files sent by a specific user ?</p>
  
  <p>I'm using Werkzeug 0.9.4 and flask 0.10.1.</p>