7 – Error copying file. Is there a permissions issue?

I have a process that fetches a file, then moves it (overwriting if already exists). The copy part fails, and I cannot determine why.

Here is some PHP I am using to reproduce the issue I am seeing (with output functions from Devel module):

$source = 'temporary://someFile';
$destination = 'public://profile_pictures/someFile.jpg';

# Get name of Linux user running this
$username = posix_getpwuid(posix_geteuid())('name');
dpm($username);   # nginx

dvm(file_exists($source));       # TRUE
dvm(file_exists($destination));  # TRUE

# File permissions
dpm(substr(sprintf('%o', fileperms($source)), -4));       # 0600
dpm(substr(sprintf('%o', fileperms($destination)), -4));  # 0644

# Try Drupal's copy
$r = file_unmanaged_copy($source, $destination, FILE_EXISTS_REPLACE);
dvm($r);  # FALSE

# Try PHP copy that Drupal uses internally, to see the error
dvm(copy($destination, $source));  # TRUE    ( Copy backwards works... )
dvm(copy($source, $destination));  # FALSE   ( But this doesn't work.  )

The last line gives the following error:

Warning: copy(public://profile_pictures/someFile.jpg): failed to open stream: “DrupalPublicStreamWrapper::stream_open”

So copying backwards works, but it won’t copy from temporary:// to profile_pictures. Also, if I delete public://profile_pictures/someFile.jpg first, then it works.

Permissions of files and folders:

drwxrwx--- www-data www-data temporary://
-rw------- nginx    nginx    temporary://someFile
drwxrwxr-x www-data www-data public://profile_pictures
-rw-rw-r-- nginx    nginx    public://profile_pictures/someFile.jpg

The temporary directory is a sub-folder of public://. The nginx user is in the www-data group.

Is this a permissions issue? Looking at Drupal’s requirements, I don’t see what I am doing wrong.