Creation of entity with image field I would like to generate an entity that contains image generated by another module file in drupal 7. This is my code :

//appel de la variable user
global $user;

//recuperation de lapercu
$apercu = $_SESSION['image_generate'];
//creation de lobject file
$file_apercu = new StdClass();
$file_apercu-&gt;uid = $user-&gt;uid;
$file_apercu-&gt;uri = $apercu;
$file_apercu-&gt;filemime = file_get_mimetype($file_apercu-&gt;uri);
$file_apercu-&gt;status = 1;
//sauvegarde en base
$file_apercu = file_save($file_apercu); 
$file_apercu = (array) $file_apercu;
$file_apercu = array($file_apercu);

//recuperation du pdf
$pdf = $_SESSION['pdf']-&gt;uri;
//creation de lobject file
$file_pdf = new StdClass();
$file_pdf-&gt;uid = $user-&gt;uid;
$file_pdf-&gt;uri = $pdf;
$file_pdf-&gt;filemime = file_get_mimetype($file_pdf-&gt;uri);
$file_pdf-&gt;status = 1;
//sauvegarde en base
$file_pdf = file_save($file_pdf);
$file_pdf = (array) $file_pdf;
$file_pdf = array($file_pdf);

//recuperation du numero de commande
$form_panier = $_SESSION['form_state_panier'];
$sql = db_query('SELECT MAX("order_id") 
                FROM commerce_order
                WHERE uid = '.$user-&gt;uid.'')-&gt;fetchAssoc();

//creation de lentity
$values = array(
            'type' =&gt; 'commandeflyer',
            'uid' =&gt; intval($user-&gt;uid),
            'status' =&gt; 1,
            'comment' =&gt; 0,
            'promote' =&gt; 0,
        );

$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper-&gt;title-&gt;set('commandeflyer'.$sql['MAX("order_id")']);
$ewrapper-&gt;field_flyer_image_commande-&gt;set($file_apercu);
$ewrapper-&gt;field_pdf-&gt;set($file_pdf);
$ewrapper-&gt;field_numcommande-&gt;set(intval($sql['MAX("order_id")']));
$ewrapper-&gt;save(true);
entity_save('node', $entity);


When I run this code I get this error :

EntityMetadataWrapperException : Invalid data value given. Be sure it matches the required data type and format. dans EntityMetadataWrapper-&gt;set() (ligne 122 dans /var/www/commerce_kickstart-7.x-2.9/profiles/commerce_kickstart/modules/contrib/entity/includes/entity.wrapper.inc).


But my type correspond : pdf for pdf and jpg for image. Thank you for help.
Solution found :

//appel de la variable user
global $user;

//recuperation de lapercu
$apercu = $_SESSION['image_generate'];
//creation de lobject file
$file_apercu = new StdClass();
$file_apercu-&gt;uid = $user-&gt;uid;
$file_apercu-&gt;uri = $apercu;
$file_apercu-&gt;filename = basename($file_apercu-&gt;uri);
$file_apercu-&gt;filemime = file_get_mimetype($file_apercu-&gt;uri);
$file_apercu-&gt;status = 1;
//sauvegarde en base
$file_apercu = file_save($file_apercu); 

//recuperation du pdf
$pdf = $_SESSION['pdf']-&gt;uri;
//creation de lobject file
$file_pdf = new StdClass();
$file_pdf-&gt;uid = $user-&gt;uid;
$file_pdf-&gt;uri = $pdf;
$file_pdf-&gt;filename = basename($file_pdf-&gt;uri);
$file_pdf-&gt;filemime = file_get_mimetype($file_pdf-&gt;uri);
$file_pdf-&gt;status = 1;
//sauvegarde en base
$file_pdf = file_save($file_pdf);

//recuperation du numero de commande
$form_panier = $_SESSION['form_state_panier'];
$sql = db_query('SELECT MAX("order_id") 
                FROM commerce_order
                WHERE uid = '.$user-&gt;uid.'')-&gt;fetchAssoc();

//creation de lentity
$values = array(
            'type' =&gt; 'commandeflyer',
            'uid' =&gt; intval($user-&gt;uid),
            'status' =&gt; 1,
            'comment' =&gt; 0,
            'promote' =&gt; 0,
        );

//creation dentity sans limage et le pdf
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper-&gt;title-&gt;set('commandeflyer'.$sql['MAX("order_id")']);
$ewrapper-&gt;field_numcommande-&gt;set(intval($sql['MAX("order_id")']));
$ewrapper-&gt;save(true);
entity_save('node', $entity);

//chargement de lentity
$commandeflyer = node_load($nid = $entity-&gt;nid, $vid = NULL, $reset = FALSE);

//ajout image
$commandeflyer-&gt;field_flyer_image_commande[LANGUAGE_NONE][] = array(
    'fid' =&gt; $file_apercu-&gt;fid,
    'uid' =&gt; $file_apercu-&gt;uid,
    'uri' =&gt; $file_apercu-&gt;uri,
    'filename' =&gt; $file_apercu-&gt;filename,
    'filemime' =&gt; $file_apercu-&gt;filemime,
    'status' =&gt; $file_apercu-&gt;status
    );

//ajout pdf
$commandeflyer-&gt;field_pdf[LANGUAGE_NONE][] = array(
    'fid' =&gt; $file_pdf-&gt;fid,
    'display' =&gt; '1',
    'uid' =&gt; $file_pdf-&gt;uid,
    'uri' =&gt; $file_pdf-&gt;uri,
    'filename' =&gt; $file_pdf-&gt;filename,
    'filemime' =&gt; $file_pdf-&gt;filemime,
    'status' =&gt; $file_pdf-&gt;status
    );

//save entity
node_save($commandeflyer);