Kat
Kat

Reputation: 1

Drupal 7 Importing images from a csv file

I have written a custom module to import users from a csv file into a drupal 7 database. The csv file has a field for the users avatar that references an image in a directory.

The issue I'm having is with attaching the images to the user. So the image gets saved correctly in the pictures directory, the record is added the file_managed table, and the file id is added to the user's record. However when I go to edit the user via the drupal interface, the picture does not appear on that page.

The code I've got is below, any help would be awesome!

$userobj = user_load(1);
$file_temp = file_get_contents('/avatars/'.$importfile);            
$file_temp = file_save_data($file_temp, 'public://pictures/' . $filename, FILE_EXISTS_RENAME);

$userobj->picture->fid = $file_temp->fid;
$userobj->status = 1;

user_save((object) array('uid' => $record->uid), (array) $userobj);

Upvotes: 0

Views: 1386

Answers (1)

ndmaque
ndmaque

Reputation: 11

best way to get file path in d7 is

$path = file_default_scheme() . '://' ;

the path should now look something like public://

i had a mare saving in the users pictures folder but found it was a file permissions thing and had to chmod the folder

Upvotes: 1

Related Questions