Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal - Get image from url and import it into node

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.

like image 489
Ziftion Avatar asked Oct 11 '11 16:10

Ziftion


2 Answers

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());
like image 92
Clive Avatar answered Sep 21 '22 05:09

Clive


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

like image 21
Stephan Avatar answered Sep 20 '22 05:09

Stephan