43

I wrote a django app, but I have a little problem with the file permissions of the uploads files from a web form.

Basically I can upload a .mp3 file but it always keep chmod 600.

The container folder has chmod 775, and the umask is set to 022.

I'm in a shared hosting service.

MackM
  • 2,906
  • 5
  • 31
  • 45
z3a
  • 1,014
  • 2
  • 11
  • 20

2 Answers2

90

Try this in your settings.py if you use Python 2:

FILE_UPLOAD_PERMISSIONS = 0644

In Python 3 octal numbers must start with 0o so the line would be:

FILE_UPLOAD_PERMISSIONS = 0o644

For more details see the documentation.

pyjavo
  • 1,598
  • 2
  • 23
  • 41
Van Gale
  • 43,536
  • 9
  • 71
  • 81
  • 8
    In case someone needs the link to the docs: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_PERMISSIONS – Akseli Palén Feb 11 '13 at 18:54
  • This is helps me too... Thank you. – Sencer H. Feb 18 '14 at 13:17
  • [Numbers starting with 0 are base 8](https://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python). The prefix [`0o`](http://stackoverflow.com/a/11620194/1888983) is an alternative, that the docs mention. – jozxyqk Nov 22 '15 at 07:53
  • 8
    In python 3 octal numbers must start with `0o` so the line would be: `FILE_UPLOAD_PERMISSIONS = 0o644` – Caumons Sep 30 '16 at 14:26
  • when I try to print my setting.UPLOAD_FILE_PERMISSIONS, it is null? – paradox Nov 08 '17 at 22:44
0

Hope this is useful. The below method can be used. This has 2 other advantages other than resolving permission errors.

  • No issues with file permissions
  • More faster
  • The file is not copied to /tmp/ folder for files which are more than 2.5 MB (saving space as well).

with open(file_name, 'wb+') as temp_file:
    for chunk in up_file.chunks():
        temp_file.write(chunk)
SuperNova
  • 25,512
  • 7
  • 93
  • 64