Last active
December 24, 2015 11:59
-
-
Save mrded/6794373 to your computer and use it in GitHub Desktop.
Upload a file to Drupal server from an outside URL
This file contains 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
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