I have the following code.

$main_image = glob('main*.{jpeg,gif,png,jpg,JPEG,JPG,PNG}', GLOB_BRACE);
if (count($main_image) != 1) 
    die("Could not find main picture !");  

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

  • 2
    Function file_copy and called by it function file_unmanaged_copy in case of errors write messages to the system log. Did you look at the "Recent log messages" report? Most likely you will find answer there... Aug 10 '16 at 22:22
  • Check your files directory is writable or not. If not then give it to write permission.
    – hizbul25
    Aug 11 '16 at 7:37
  • The files folder has 777 rights. Aug 11 '16 at 7:53
  • How about the directory permission on /sites/default/files/galerii_imagini ?
    – Jimmy Ko
    Aug 14 '16 at 0:28
  • 2
    @applecrusher This is very bad advice.
    – JayIsTooCommon
    Aug 18 '16 at 7:37

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.

  • "galerii_imagini" exists and it's a directory. Is writable by drupal because I can create manual galleries that store the images in this directory. Because I am on a shared hosting and I had rights problem in the past I am not sure that what I am seeing in the filezilla is correct, regarding the rights. Aug 20 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.