Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a file with Drupal 8

By editor , 28 November, 2015
Question

How to create a file programmatically in Drupal 8 ?

Example:

// Programmatically create files.
use Drupal\file\Entity\File;
$file = File::create([
  'uid' => 1,
  'filename' => 'test.txt',
  'uri' => 'public://page/test.txt',
  'status' => 1,
]);
$file->save();

NOTE : This will not create a file in the DB but not in the disk.
To save data: You must do like that:
    $dir = dirname($file->getFileUri());
    if (!file_exists($dir)) {
      mkdir($dir, 0770, TRUE);
    }
    file_put_contents($file->getFileUri(), "test");
   $file->save();

And also:
        $file_usage = \Drupal::service('file.usage');
        $file_usage->add($file, 'mymodule', 'user',$uid);
        $file->save();

Solution 2.
You can create a file and save data at once.
$file = file_save_data($value, "public://$dir/$filename", FILE_EXISTS_RENAME);

Get the real file path:
$path = drupal_realpath($file->getFileUri());

It may also work in this way (not tested)

$handle = fopen('<path to local file>', 'r');
$file = file_save_data($handle, 'public://<filename>');
fclose($handle);

Drupal File folders:

Drupal use 3 folders to store file.

private://    Private files directory
public://    Public files directory
temporary://    Tempory files directory

Tags

  • Drupal 8
  • Code
  • Add new comment

Comments5

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Nick (not verified)

9 years 7 months ago

not twice

"NOTE : This will not create a file in the DB but not in the disk."

  • Reply

James (not verified)

9 years 3 months ago

In reply to not twice by Nick (not verified)

Agreed, it should probably…

Agreed, it should probably say, "Note: This will create a file in the DB but not on the disk."

  • Reply

Joakim Calais (not verified)

8 years 11 months ago

mistake in text?

"NOTE : This will not create a file in the DB but not in the disk.
To save data: You must do like that:"

I think there is one "not" too many in that sentence.

  • Reply

mohamed (not verified)

8 years 8 months ago

Read avatar user and diplay it

hi , i want to get th avatar user and display it, can you help me please . 

  • Reply

Alessandro (not verified)

4 years 10 months ago

In Drupal 9.3.3 doesn't work

Hi all.

The first solution in Drupal 9.3.3 seem doesn't work. I obtain two errors:
- Drupal\Core\File\Exception\FileNotExistsException: File 'private://pdf/yyy.pdf' could not be copied because it does not exist. in Drupal\Core\File\FileSystem->prepareDestination() (linea 459 di /var/www/html/test/web/core/lib/Drupal/Core/File/FileSystem.php)
- Drupal\Core\Entity\EntityStorageException: File 'private://pdf/yyy.pdf' could not be copied because it does not exist. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (linea 811 di /var/www/html/test/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php)

Anyone have any idea? Maybe in 9.3.x has changed something in File management?

Alex.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer