I am trying to attach a file in the custom module's directory to every new node created for a content type. How do I create the file object? The following code gives me an error.

$uri1  = file_unmanaged_copy('checklist1.pdf', 'public://checklist1.pdf', FILE_EXISTS_REPLACE);

$file1 = File::Create([
    'uri' => $uri1,

The error I get is the following one:

The specified file checklist1.pdf could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.

To add, this code is in the module file inside a hook.

1 Answer 1


You need to specify the path to your module:

$module_path = drupal_get_path('module', 'module_name');

$uri1  = file_unmanaged_copy("$module_path/checklist1.pdf", 'public://checklist1.pdf', FILE_EXISTS_REPLACE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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