Skip to main content
edited tags
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

How todo I create a file object from a local file?

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,
]);
$file1->save();

ErrorThe 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 .modulemodule file inside of a hook.

How to create a file object from a local file?

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 gives me an error:

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

$file1 = File::Create([
    'uri' => $uri1,
]);
$file1->save();

Error:

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 of a hook.

How do I create a file object from a local file?

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,
]);
$file1->save();

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.

Source Link

How to create a file object from a local file?

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 gives me an error:

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

$file1 = File::Create([
    'uri' => $uri1,
]);
$file1->save();

Error:

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 of a hook.