views:

19

answers:

1

With print_r I get:

stdClass Object
(

 [field_car_image] => Array
    (
        [0] => Array
            (
                [filename] => HERMAN 096.jpg
                [filepath] => sites/default/files/HERMAN 096.jpg
                [filemime] => image/jpeg
                [filesize] => 933105
                [status] => 1
            )

    )
 ..... // the rest is also on here

It saves ALL the other data perfectly, just not the image. After:

node_object_prepare($node); 
$node = node_submit($node);
node_save($node);

I do a print_r on the $node object, and there is no reference to "field_car_image". Anybody know how to properly save file fields?

+1  A: 

You're missing the fid (file ID) of the table files in the filefield array; To upload correctly a file in drupal be sure to use file_save_upload function http://api.drupal.org/api/function/file_save_upload/6

This code shows you how to save a filefield into the node.

 $node->field_car_image = array(0=>array(
            'fid'=>$fid,//get this value from the file object returned by file_save_upload
            'uid'=>$uid,//user ID
            'filename'=>$filename,
            'filepath'=>$filepath,
            'filemime'=>$filemime,
            'filesize'=>$filesize,
            'status'=>$status,
            'timestamp'=>$timestamp,
        ),
    );
Bladedu
Ok, but how do you use the file_save_upload function, if you aren't actually doing an upload? I already have the file in the right location. Must I just manually create an entry in the files table?
RD
Are you trying to create the node object programmatically? because my solution was for this purpose. What i mean is: you have a custom form where ppl upload your file and other information. once they click on save you build a new $node object with the passed information. If you're trying to achieve something else, please explain better the context :)
Bladedu