3

I was having this problem: Files uploaded via custom configuration form are eventually lost.

So, I used the solution, basically:

$file->setPermanent();
$file->save();

But then, even thou $file->isTemporary() is FALSE, the URI and URL seem to indicate that the file is still temporary:

URI: $file->getFileUri() yields temporary://filename.jpg

URL: file_create_url($file->getFileUri()) yields /system/temporary?file=filename.jpg

1

You need to mark file as "used", there is file.usage service for that, you can do it by next code: \Drupal::service('file.usage')->add($file), but after removing file from config or changing it by another file don't forget to remove file usage by \Drupal::service('file.usage')->delete($file), otherwise file would not be removed from file system.

In drupal 8.3 default behavior about unused files was changed and by default all unused files should not be deleted, until variable make_unused_managed_files_temporary is true.

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.