Cannot access pdf files in private directory

Drupal version: 9.3.3

Issue:

PDF files (.pdf) uploaded to private file system result in a 404 error when trying to view. This occurs even for an admin user with Administrator permissions. Uploaded .txt, .doc, .docx files can be viewed with no issue. This issue is occurring in a production environment – site is hosted on Cloudways

Private directory set in settings.php

- $settings['file_private_path'] = $app_root . '/../private'; 

Steps to reproduce:

  • Login to site as admin (Administrator role)
  • Add file field to Article content type
  • Set file field "Upload destination" to "Private files"
  • Set "Allowed file extensions" is set to txt, pdf, doc, docx. Maximum upload size is set to 1 MB
  • Set File directory to article-test
  • My Drupal web root is public_html/web
  • This means private files should be uploaded to public_html/private/article-test

Then:

  • Create an Article (Test article)
  • Add Title, Body and upload a valid 150 KB pdf file (doc_0.pdf)
  • Save and publish Article
  • View Test article
  • Click on uploaded doc_0.pdf (file link shows up as https://MYSITE.com/system/files/article-test/doc_0.pdf)
  • Get a 404 Page not found error
  • Browser bar shows URL as https://placerstaffing.com/user/login?destination=/system/files/article-test/doc_0.pdf
  • Go to https://MYSITE.com/admin/content/files
  • Click on the file link https://placerstaffing.com/system/files/article-test/doc_0.pdf
  • Get same exact 404 error as above
  • dblog reports:

    page not found Location https://MYSITE.com/user/login?destination=%2Fsystem%2Ffiles%2Farticle-test%2Fdoc_0.pdf

  • Remove doc_0.pdf file from Test article and upload a text file (with .txt) extension or a MS word file (with .doc or .docx extension) and viewing the file is fine – NO 404 error.

    • When I view the file .txt or .doc or .docx file, I’m prompted to save or view it.
    • I don’t believe this is a permissions issue, otherwise I wouldn’t be able to access these other file types.
    • public_html/private dir and lower dirs permissions are 775
    • Uploaded file (.pdf, .txt, .doc, .docx) permissions are 664

I’m wondering if there’s a PHP setting or Drupal private file setting or server setting that’s missing to allow .pdf files to be viewed ?

There is an .htaccess file in public_html/private directory. Thinking that may be related to the issue, I temporarily renamed it .htaccess.saved and tried to access doc_0.pdf file with same result.

Any help in resolving this would be greatly appreciated!

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

Cannot access pdf files in private directory

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.