Skip to content

Instantly share code, notes, and snippets.

@edutrul
Last active December 2, 2022 12:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edutrul/9d04d7742545dbedd1a36f7b17632b7a to your computer and use it in GitHub Desktop.
Save edutrul/9d04d7742545dbedd1a36f7b17632b7a to your computer and use it in GitHub Desktop.
How to download a file programmatically using guzzle in Drupal 8 / Drupal 9
<?php
$external_file = 'https://www.example.com/test.png'
$destination = 'sites/default/files/a-directory/test.png';
/** @var GuzzleHttp\Psr\Response $response */
$response = \Drupal::httpClient()->get($external_file, ['sink' => $destination]);
// file gets downloaded under /sites/default/files/a-directory/test.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment