How to attach an image to a node

The following examples assumes a node already exists, and you already have the ID for that node. You also need to make sure you have the Image Cache and Image Field modules installed along with the CCK module.

First we load our node:

<?php$node = node_load($nid);?>

In this example, I will also update some fields such as the uid and name.

<?php$node->uid = '1';$node->name = 'admin';?>

Now for the interesting part, the image attachment. You must already have the image uploaded on the server somewhere. This image will not be removed, you must do that afterwards if needed. The following will make a copy of that image and place it in Drupal's files directory.

The first thing we do is read the image data, I placed my images in an "upload" folder one level above the Drupal installation.

<?php$file_temp = file_get_contents('../upload/mypicture1.jpg');?>

Next I use Drupal's built-in function's to copy the image to the site's files directory. If a similarly named file already exists, it will be renamed. The complete path to the new file is returned.

<?php$dest = file_directory_path() . '/mypicture1.jpg';$file_temp = file_save_data($file_temp, $dest, FILE_EXISTS_RENAME);?>

The next step is to create the image data structure, it's just an associative array with the file details. Fields such as title, alt, filepath, filename, filesize, and filemime should be self explanatory. The field name 'nid' is the ID of the node you want to attach the image to.

Since this is a new image/file we must generate a file ID, this goes in the field named 'fid'. Drupal provides a function for just that purpose: 'db_next_id()'. It takes one parameter in the following format: "{files}_fid", and returns a 'fid' we can utilize.

<?php$image = array(                            'fid'=>db_next_id('{files}_fid'),                    'title' => "My First Drupal Picture",                     'alt' => "myimage1",                     'nid' => $nid,                     'filename' => basename($file_temp),                     'filepath' => $file_temp,                     'filemime' => 'image/jpg',                     'filesize' => filesize($file_temp) );?>

Now we save the file's information to the Drupal database, unfortunately, their is no built-in Drupal function to do that automatically (you'd think their would be, but their isn't). Thankfully, Drupal makes manipulating the database a one liner:

<?phpdb_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s','%s','%s',%d)", $image['fid'], $image['nid'], $image['filename'], $image['filepath'], $image['filemime'], $image['filesize'] );?>

Now that my file exists in the Drupal system, I assign it to the node object, and finally save the node using the node_save() function.

<?php$node->field_image_cache = array($image);node_save($node);?>

Comments

Tue, 05/18/2010 - 14:01

Heads up to future googlers, this code is for D5 ( the db_next_id() function is not available in D6, for instance).

If I can get it working, I'll post an updated version...

Tue, 05/18/2010 - 16:27

wealth of info for D6 here:

http://drupal.org/node/330421