Programmatically inserting files for CCK content

Blackguard - March 13, 2008 - 19:29
Project:Content Construction Kit (CCK)
Version:5.x-1.6
Component:General
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I need to import image files to nodes.

I have a cck node type 'profile' with a field 'signature_img', an image.

I'd like to upload all the images to my file directory, and insert the appropriate values in the database tables for these images to show up with the nodes.

Should I go :

$this_node = node_load($nid); // for each profile node
$this_node->field_signature_img[0]['filepath'] = 'files/etc.jpg';
$this_node->field_signature_img[0]['etc']; // for each value of the field;
node_save($this);

?

#1

Blackguard - March 13, 2008 - 19:47

Or is there a way to add the image file references myself directly into the database? By this I mean insert the information into the files table (filename, etc), get the fid from the insert, add the information in the appropriate row of the content_type_profile table?

#2

yched - March 13, 2008 - 20:01

You might want to look at http://drupal.org/node/201594

Thanks for closing this issue the link above fixes it for you :-)

#3

Blackguard - March 13, 2008 - 20:16

This script will take care of the files table, the specific content_type table, file revision if necessary, etc?

#4

yched - March 13, 2008 - 20:32

This script should create a new node and fill an 'imagefield' with a image. The file has to be present on your server already. Aside from that, it should populate all the right tables correctly.
Last time I gave it a try a few months ago it worked fairly well, but you should probably try it for yourself and adapt for your needs. This is only a starting point.

#5

Blackguard - March 13, 2008 - 20:40

Thanks for pointing me to it.

I adapted it a bit because I'm importing the image to fields of already existing nodes.

I tried it : the script saves the file in the appropriate folder, populates the fields in the table for the content_type, but does not add the file info to the files table. It does not provide a fid (file id) to the content_type table. fid = 0 for this image after I run the script. So it's not working yet, and I assume at this point those are the reasons why - but I might be completely wrong :)

It's a good start, but I'm still missing important pieces about how to insert a file for a cck node field. And I don't know what these pieces are ... yet :)

#6

Blackguard - March 13, 2008 - 21:03
Status:active» fixed

Found it here!

http://drupal.org/node/136257

I don't understand it but I suppose it's part of CCK. What's missing is :

'fid' => 'upload',

#7

yched - March 13, 2008 - 21:08

Cool, thanks for reporting back. I updated the handbook page with this tip.

#8

Blackguard - March 13, 2008 - 21:16

Avec plaisir :)

#9

Blackguard - March 14, 2008 - 04:00
Status:fixed» active

It seems my problem is not solved.

The operation works only for a single node, or a single file import.

When I use the following code, only the last file of the loop gets properly inserted and written. All other files are 0 k (zero k files). I read the information from a text file, and however many lines I run through the script, only the last one is successfully processed.

$fichierTexte = "signature_img_test.txt";
$delimiter = "*";

$lines = file($fichierTexte);

foreach ($lines as $lineAr)
{

  $arMembres[] = explode($delimiter, $lineAr);
 
}

foreach ($arMembres as $uneLigne => $unMembre)
{
 
  // get uid for username
  $sql = "SELECT uid FROM users WHERE name = '$unMembre[0]'";
  $res = mysql_query($sql);
  $uid = mysql_result($res, 0);
 
  // get profil nid
  $sql = "SELECT nid FROM node WHERE uid = $uid AND type = 'profil'";
  $res = mysql_query($sql);
  $nid = mysql_result($res, 0);
 
  // get profile node
  $node = new StdClass();
  $node = node_load($nid);
 
  $file_temp = file_get_contents("./files/signatureT/$unMembre[1]");
  $file_temp = file_save_data($file_temp, file_directory_path() ."/signature/$unMembre[1]", FILE_EXISTS_RENAME);
  $node->field_signature_image = array(
    array(
      'fid' => 'upload',
      'title' => basename($file_temp),
      'filename' => basename($file_temp),
      'filepath' => $file_temp,
      'filesize' => filesize($file_temp),
    ),
  );

 
  node_save($node);

}

#10

Blackguard - March 14, 2008 - 15:39

Apparently this could be a server issue. I used brute force to make this work in the end, but I can't say I was 100% successful with this script.

#11

Blackguard - March 14, 2008 - 15:39
Status:active» closed
 
 

Drupal is a registered trademark of Dries Buytaert.