0

I'm using Drupal 7.56.. I want to upload images from other urls.. I use:

$image_original_path = '.. here my original path..';
$my_destination = 'sites/default/files/images/';
$my_new_name = 'my_image.jpg';
$filename = $my_destination.$my_new_name;
$image_object = file_get_contents($image_original_path);
$file = file_save_data($image_object,  $base_url.'/'.$filename, FILE_EXISTS_RENAME);

But the image is not uploaded.. Any idea please? Thank you..

2 Answers 2

0

Updated:
Try with this code:


$image_original_path = '.. here my original path..';
$image_object = file_get_contents($image_original_path);
$file = file_save_data($image_object, file_default_scheme().'://'.basename($image_original_path),FILE_EXISTS_RENAME);
// Add this code also
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);
}

8
  • Thank you.. But it doesn't work!!
    – A.Sana
    Commented Feb 21, 2018 at 9:05
  • @sana Please try the updated code. Commented Feb 21, 2018 at 9:19
  • Thank you.. But after running I have this error "The website has encountered an unexpected error. Please try again later."
    – A.Sana
    Commented Feb 21, 2018 at 9:41
  • Please check the code because this code is working fine at my end. Commented Feb 21, 2018 at 10:00
  • Is your problem solved? Commented Feb 21, 2018 at 10:34
0

My problem is fixed by

  1. going to admin/config/media/file-system and save configurations with temporary folder = tmp
  2. check permissions 777 on tmp folder
  3. $filename = 'my_image.jpg'; $image_object = file_get_contents('...original_image_s_path...'); $file = file_save_data($image_object, 'public://' . $filename, FILE_EXISTS_RENAME);

This saves my image under sites/default/files

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.