foreach ($imageurl as $imagechange) {
           $image =$imagechange[$i];
           $file = system_retrieve_file($image,'public://beachesmigration/', TRUE, $replace = FILE_EXISTS_REPLACE);
           echo "<pre>";print_r($file);echo "</pre>"; exit;
           file_usage_add($file, 'iks_beaches', 'beaches', $file->fid);
  • what is the problem in the above code? Are u getting any errors? Pls mention
    – harshal
    Commented Aug 26, 2013 at 6:02
  • i use the above code but no folder creation takes place :(
    – Ajmal
    Commented Aug 27, 2013 at 8:53
  • You can use drupal_mkdir() to create new folder in the code above Link : api.drupal.org/api/drupal/includes!file.inc/function/…
    – harshal
    Commented Aug 27, 2013 at 9:01

2 Answers 2


Hope this will help you.

In Drupal 7 , system_retrieve_file function will download a file from a remote source, copy it from temp to a specified destination and optionally save it to the file_managed table if you want it to be managed.

  • You need to create a folder beachesmigration inside sites\default\files.
  • By using system_retrieve_file it will be store your images inside beachesmigration folder with the filename you no need to specify the filename manually.

 $image = 'http://wordpress.ikitesurf.com/wp-content/uploads/2012/09/2032239599_fdce5e0ab0_z.jpg';
//I have used picture folder to store images using image field settings
 $file = system_retrieve_file($image, 'public://beachesmigration/', TRUE, $replace = FILE_EXISTS_REPLACE);



Try this :

function savefile($filepath, $uid = 1) {
  global $base_url;
  if (filesize($filepath) == 0 ) {
    return array();
  $file = (object) array(
    'uid' => $uid,
    'uri' => $filepath,
    'filemime' => file_get_mimetype($filepath),
    'status' => 1,
  if(file_exists($filepath)) {
    $file = file_copy($file, 'public://');   //Change path here
    return $file;
  } else {
    return array();

$file = savefile("http://wordpress.ikitesurf.com/wp-content/uploads/2012/09/2032239599_fdce5e0ab0_z.jpg");
  • Hello. Yuor answer does not mention $pathval or beachesmigration, so how it is supposed to help?
    – Mołot
    Commented Aug 26, 2013 at 7:00

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.