Files could not be uploaded from form

Created on 4 April 2024, 2 months ago
Updated 9 April 2024, 2 months ago

Problem/Motivation

I am working on creating a custom module for creating Nodes according to PDF. Basically I want the user to upload a PDF, and then to take the formatted text with all the styles and font, and put it in a field in a Node of a Content Type.

Now more specifically about my problem. Firstly I have changed my temporary files directory as so:

$settings['file_temp_path'] = '../tmp';

Next thing I did is create a form where the file is uploaded:

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['pdf_file'] = array(
      '#type' => 'file',
      '#title' => $this->t('Upload PDF File'),
      '#description' => $this->t('Upload a PDF file containing decisions.'),
      '#required' => TRUE,
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Import PDF and Create Decisions'),
    );

    return $form;
  }

Note that still I do not have any problems until here

Now I start having problems. By looking at my temp directory, before uploading a file from my form I cannot see any files in it. Now when I press the Upload button, the file is created in my temp directory, I can see that the pdf is there, but then I get this error in Drupal

Error message

The specified file helloworld.pdf could not be uploaded.
  • The file temporary://helloworld_4.pdf already exists. Enter a unique file URI.

I find this really weird because there were no files, and then when I uploaded it, I feel like it first created the file there, and then it saw that it's already there and maybe something like that cause this problem.

Even if there was the file with name helloworld.pdf from a previous import, Drupal successfully changes the name of the file to helloworld.pdf_0, then helloworld.pdf_1 etc..

And I still get the same error with every try I do.

Now when I try to see the uploaded file from my submit function:

public function submitForm(array &$form, FormStateInterface $form_state) {
  $file_pdf = file_save_upload('pdf_file', [
      'file_validate_extensions' => ['pdf'],
  ], 'temporary://');
}

$file_pdf always is equal to:

array:1 [β–Ό
  0 => false
]

I suppose it makes sense to see this since the file was not uploaded, but I really don't understand why the file is not uploaded because when i go in my directory, I can successfully see it there created after the submit button was pressed.

Note that before changing $settings['file_temp_path'] = '../tmp';

There was not problem with this. The reason now why I have changed this is because later on, I want to take that file and execute the command: pdftohtml -s -i -noframes -stdout helloworld.pdf which will create me a .html file with the contents of the pdf so that I can use the contents of that html file to add in my field.

But I really cannot see the problem here, I mean why would it break just because I changed the temporary files directory. Note that the folder is there and its okay because as mentioned, I can see the files being created in my directory after the submit button was pressed.


I have attached a video demonstration of my problem

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ’¬ Support request
Status

Active

Version

10.2 ✨

Component
File moduleΒ  β†’

Last updated 3 days ago

Created by

πŸ‡¨πŸ‡ΎCyprus chrisk8r

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024