Skip to content

Instantly share code, notes, and snippets.

@mrded
Last active December 24, 2015 11:59
Show Gist options
  • Save mrded/6794373 to your computer and use it in GitHub Desktop.
Save mrded/6794373 to your computer and use it in GitHub Desktop.
Upload a file to Drupal server from an outside URL
function upload_file($path) {
// This is a PHP function to get a string representation of the image file.
$image = file_get_contents($path);
// A stream wrapper path where you want this image to reside on your file system including the desired filename.
$destination = 'public://path/to/store/this/image/name.jpg';
$file = file_save_data($image, $destination, FILE_EXISTS_REPLACE);
if (is_object($file)) { // if you get back a Drupal $file object, everything went as expected so make the status permenant
$file->status = 1;
$file = file_save($file);
}
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment