There’s a secutiry problem if you set 777 to one folder.
Maybe you can set regular permissions, but changing the owner of the folder.
For example, apply something like this:
chown -R www-data:www-data media
With that change, your folder only gonna be writable for the owner, in this case, www-data, that is the user that (it depends of each configuration) runs your webserver.