11

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],
  );

  $destination = "public://galerii_imagini/" . $file->filename;

  if ($file = file_copy($file, $destination, FILE_EXISTS_REPLACE)) {}
  else {
    die("Could not copy " . $file_path . " in " . $destination);
  }
}

In the Drupal logs, the error message is the following.

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?
In /admin/config/media/file-system I set the the path for public files (sites/default/files) and set the file permissions for that directory recursively to 777.

12
  • 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, 2016 at 22:22
  • Check your files directory is writable or not. If not then give it to write permission.
    – hizbul25
    Aug 11, 2016 at 7:37
  • How about the directory permission on /sites/default/files/galerii_imagini ?
    – Jimmy Ko
    Aug 14, 2016 at 0:28
  • 777 on both directory and files inside Aug 17, 2016 at 14:23
  • 2
    @applecrusher This is very bad advice.
    – JayIsTooCommon
    Aug 18, 2016 at 7:37

0

Browse other questions tagged or ask your own question.