0

what i want to perform (globally) is to import a csv file that contains data such as book_title, author, and the cover image of the books. The csv file is loaded, book_title and author are stored in their respective tables but for pictures, what i want is to put them manually (ftp for example) on a folder (doesn't matter if it's my default public folder or a new one) and store data in tables like : file_managed and field_data_field_cover_picture etc In my csv file, and for pictures column i just write filenames of every picture.

At the moment, i'm confused and this is my actual code :

$filename_cover = $get_filename.'.jpg'; <===from csv

        $add_cover_file = db_insert('file_managed')
        // ->key(array('fid' => rand()))
        ->fields(array(
            'uid' => $user->uid,
            'filename' => $filename_cover ,
            'uri' => 'public://couvertures/'.$filename_couv,<==my file 
            'filemime' => 'application/octet-stream',
            'status' => 1,
            'timestamp' => time()
        ))
        ->execute();

Now adding on the pictures covers table : (how to put the fid ?)

  $add_couverture = db_merge('field_data_field_photo_de_couverture')
        ->key(array('entity_id' => $newNode->nid, 'field_photo_de_couverture_fid'=>$add_couv_file->fid ))
        ->fields(array(
            'field_photo_de_couverture_fid'=>$get_fid ,
            'entity_id' => $newNode->nid,
            'delta' => 0,
            'bundle' => 'livre',
            'entity_type' => 'node'
        ))
        ->execute();
1

Try this:

$file = file_save((object)array(
    'filename' => $file_name,
    'uri' => $destination_uri,
    'status' => FILE_STATUS_PERMANENT,
    'filemime' => file_get_mimetype($destination_uri),
  ));
//at that point you can use the $file->fid
//do the magic on your custom table field_data_field_photo_de_couverture
  • Thank you @Namari , i completely forgot to use $file->fid – Sushi Feb 9 '16 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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