5

Im writing a module for drupal, Im trying to create a node from my module, everything is fine , I only have 1 problem with creating an image , The image exist on different server, so I want to grab the page and insert it , I install module http://drupal.org/project/filefield_sources , which has remote option , I search in the module code , I could not find the function that he used for this process, module work very nice from interface , but how i make it do the job from code ? which function should i call and what parameter should i pass .

I'm over Drupal 6.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Ziftion
  • 51
  • 1
  • 3

2 Answers2

10

Hopefully you're using Drupal 7...

The 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.

$managed = TRUE; // Whether or not to create a Drupal file record
$path = system_retrieve_file($url, 'public://my_files/', $managed);

If you want to get the file object immediately after you've done this, the following is the only way I've found so far:

$file = file_load(db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField());
Clive
  • 36,918
  • 8
  • 87
  • 113
  • 1
    I had to change `SELECT MAX(id)` to `SELECT MAX(fid)` into the last query since the id column didn't exist in Drupal 7.17: – Patrick Peak Dec 05 '12 at 19:02
4

get fid using $path->fid. no need to mysql

Stephan
  • 594
  • 7
  • 17