file_copy() is not working I have the following code.
$main_image = glob('main*.{jpeg,gif,png,jpg,JPEG,JPG,PNG}', GLOB_BRACE);
if (count($main_image) != 1) 
    die(&quot;Could not find main picture !&quot;);  

$file_path = drupal_realpath($main_image[0]);
$file = (object) array(
            'uid' =&gt; $userID,
            'uri' =&gt; $file_path,
            'filemime' =&gt; file_get_mimetype($file_path),
            'display' =&gt; 1,
            'filename' =&gt; $main_image[0]
);
// copy the file in the correct folder
$destination = &quot;public://galerii_imagini/&quot; . $file-&gt;filename;
 if ($file = file_copy($file, $destination, FILE_EXISTS_REPLACE)) {} else {
    die(&quot;Could not copy &quot; . $file_path . &quot; in &quot; . $destination);
}

Is there something wrong with my code? I always get the message from die command inside the last if statement.
Any help will be appreciated.
Edit: Thanks to Stanislav Agapov, I found the logs from drupal.
The message is:

File ..../main_ROSE2106.JPG
could not be copied, because the destination directory
public://galerii_imagini is not configured correctly.

What should I do to configure it correctly? I have set in /admin/config/media/file-system the path sites/default/files as public files.
Is there something else to set?
Edit #2:
I set 777 for the whole /sites/default/files tree.
Are you certain that the path sites/default/files/galerii_imagini  exists and is a directory? You could see this error either if it doesn't exist or if it exists but is a file instead of a directory. If you expect Drupal to create the galerii_imagini directory, you need to add this line before your call to file_copy:

file_prepare_directory("public://galerii_imagini", FILE_CREATE_DIRECTORY);


Based on a search of the Drupal 7.x source code, the message File %file could not be copied, because the destination directory %destination is not configured correctly. is only ever produced in file_unmanaged_copy, which is called by file_copy. It calls file_prepare_directory first on the destination as passed to file_copy, then on its parent directory. It produces the error message you're seeing only if both calls fail. Since it calls file_prepare_directory with default flags, it can fail with that message if:


neither the destination path nor its parent directory exists
the destination path does not exist and its parent directory exists but is not a directory
either the destination path or its parent directory exists, is a directory, is not writable, and changing its permissions to make it writable failed


In the last case, however, you would see another message in the log immediately before the one you reported in your question: The file permissions could not be set on %uri. Since you haven't reported seeing that message, I can only assume that one of the first two cases is correct.