Access Denied for programmatically added file

I have a command that imports contents from an external database. I’m able to attach also files with this part of code:

$file = File::create([   'filename' => $new_filename,   'uri' => $folder_uri . '/' . $new_filename,// private://my_content_type/<subdir>/filename.pdf ]); $file->setPermanent(); $file->save(); 

and then attach to my new node with this part of code:

$storage_handler = Drupal::entityTypeManager()->getStorage("node"); $data = [     'type' => 'my_content_type',     'title' => 'A title',     'status' => 1,     ...     'field_attach' => ['target_id' => $file->fid->value],     ...   ]; $node = $storage_handler->create($data); $node->save(); 

Editing the new content I can correctly find the attached file but opening it in preview I receive "Access Denied", even if I’m the admin. The field field_attach is a multiple one and manually attaching another file from the edit page, I can correctly preview it.

debug:

This is the devel part for the node:

[field_attach] => Array (     [x-default] => Array     (         [0] => Array         (             [target_id] => 1876 // Can't preview, access denied             [display] => 1             [description] =>          )         [1] => Array         (             [target_id] => 1878 // Can preview it             [display] => 1             [description] =>          )     ) ) 

In database each file has same values:

# select * from file_usage where fid in(1876, 1878)G *************************** 1. row ***************************    fid: 1876 module: file   type: node     id: 92  count: 1 *************************** 2. row ***************************    fid: 1878 module: file   type: node     id: 92  count: 1 2 rows in set (0.000 sec)   # select * from file_managed where fid in(1876, 1878)G *************************** 1. row ***************************      fid: 1876     uuid: 876c1bd1-566c-4d6b-a767-4925d75b593f langcode: it      uid: 1 filename: first_file.pdf      uri: private://my_content_type/201211/first_file.pdf filemime: application/pdf filesize: 140982   status: 1  created: 1651744379  changed: 1651744379 *************************** 2. row ***************************      fid: 1878     uuid: 4c26c169-3e92-4254-af49-e5b203caf4c6 langcode: it      uid: 1 filename: second_file.pdf      uri: private://my_content_type/202205/first_file.pdf filemime: application/pdf filesize: 140982   status: 1  created: 1651745721  changed: 1651745725 2 rows in set (0.000 sec) 

Permissions are the same for directories and for files. Something is different from manual and programmatically attach. Lost to set something?

Thanks in advance.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Access Denied for programmatically added file

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.