Last active
September 20, 2016 05:48
-
-
Save liberatr/5ca65cd4f64a67170082b0d78a8a4dda to your computer and use it in GitHub Desktop.
Programmatically save a file to Drupal from your local file system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Saves a file on your local file system you want to copy to Drupal default files directory. | |
* This assumes you have bootstrapped Drupal. | |
* Originally written to be invoked within a Behat context. | |
*/ | |
use Drupal\file\Entity\File; | |
$file = File::create([ | |
'uid' => 1 | |
]); | |
$file->setFilename("image-fact.jpg"); | |
$file->setPermanent(); | |
$file->setFileUri(rtrim(realpath("/var/www/backup/files"), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file->getFilename()); | |
$file->set('langcode', $file->language()->getId()); | |
$file->setChangedTime(time()); | |
$dest = file_build_uri(drupal_basename($file->GetFileUri())); | |
$result = file_copy($file, $dest, FILE_EXISTS_REPLACE); | |
/************************* | |
* REMOTE VERSION | |
* This one helps you copy a file from a remote source to the local system, perhaps during a migrate.module migration. | |
*************************/ | |
use Drupal\file\Entity\File; | |
$this->fileSystem = \Drupal::service('file_system'); | |
/** | |
* Save the file data we retreived from external URL | |
* | |
* @param string $url | |
* The image url to fetch. | |
* | |
* @return \Drupal\file\Entity\File | |
*/ | |
function saveFile($url) { | |
$source = $url; | |
$basename = $this->fileSystem->basename($url); | |
$temp_destination = 'temporary://' . $basename; | |
$destination = $this->configuration['destination_path'] . $basename; | |
$dir = $this->getDirectory($destination); | |
if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { | |
throw new Exception("Could not create or write to directory '$dir'"); | |
} | |
if (@copy($source, $temp_destination)) { | |
$this->file->setFilename($basename); | |
$this->file->setFileUri($temp_destination); | |
$this->file->setChangedTime(time()); | |
return file_copy($this->file, $destination, FILE_EXISTS_REPLACE); | |
} | |
} | |
/** | |
* Returns the directory component of a URI or path. | |
* Copied from migrate_plus module. | |
* | |
* For URIs like public://foo.txt, the full physical path of public:// | |
* will be returned, since a scheme by itself will trip up certain file | |
* API functions (such as file_prepare_directory()). | |
* | |
* @param string $uri | |
* The URI or path. | |
* | |
* @return string|false | |
* The directory component of the path or URI, or FALSE if it could not | |
* be determined. | |
*/ | |
function getDirectory($uri) { | |
$dir = $this->fileSystem->dirname($uri); | |
if (substr($dir, -3) == '://') { | |
return $this->fileSystem->realpath($dir); | |
} | |
return $dir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment