If you need to create folders on your server that you can then write to from your web browser you may need to use at least 775. Note that this isn't necessarily the case for creating files. In fact, you probably don't want your files to be 777 at all.
You can also use bash commands within the php code if you would like to avoid having your server (aka www-data) create the directories but you'll still need to place the correct permissions on the folders to allow www-data to then write files to the directories.
Do check your ownerships. The above is based on a folder being owned by some user and group owned by www-data. You can also try to set the folder ownership to www-data but the permission would still be rwx for that particular user.
I hope that's not way to convoluted.
Commands that might help you find out who is running apache on your server:
#top
look for something like:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20610 www-data 20 0 36772 10m 4380 S 4 0.1 0:04.03 apache2
or
#ps aux | grep "apache"
look for something like
www-data 26898 0.0 0.0 34248 4076 ? S 17:59 0:00 /usr/sbin/apache2 -k start