I'm trying to guess: user executing your script doesn't seem to have the required permissions.
For instance, if a php script run by apache web server is used to upload files to a certain subdir, then that subdir should be available for read/write access to your apache user (commonly "apache" or "www-data").
: when you run a web server, like apache for instance, only the parent process runs as "root", a child process (which is actually the one you care about to run php scripts) usually runs with "www-data" uid on ubuntu/debian platforms.
ps auwx | grep apache | grep -v grep
root 1606 0.0 0.0 63520 3020 ? Ss déc.01 0:16 /usr/sbin/apache2 -k start
www-data 22325 0.0 0.0 352676 4232 ? Sl 06:25 0:02 /usr/sbin/apache2 -k start
www-data 22326 0.0 0.0 352676 4228 ? Sl 06:25 0:02 /usr/sbin/apache2 -k start
and that explains why it cannot write to your specific subdir in your public_html, if that specific subdir does not belong to that "www-data" user or group.
To fix this, for instance you can set the "group" attribute of your subdir to "www-data", and set write permission to that group:
sudo chgrp -R www-data ./public_html/my_files
sudo chmod -R g+w ./public_html/my_files
warning: By doing this, you recursively change the group attribute of ./public_html/my_files to www-data, and allow write access to users belonging to the www-data group, thus allowing apache to (over)write files in that folder.