Drupalers,

I have a large number of files (in the thousands) which I need to add to Drupal. Obviously, I'm not going to do this by hand, so I am writing a script to do this. I have figured out how to create nodes for each document (node_save), but now need to figure out how to attach the files to the node. I looked over upload.module, but didn't understand it well enough to guess which function I needed to call.

Can anyone offer some ideas how I can do this?

Thanks!!!

Comments

Cheeze’s picture

I'd like to know too.

webchuck’s picture

Cheeze,

Here's what I figured out. There's a function called upload_save($node) in upload.module. If you create a node, save it using node_save(), you can then pass that node into upload_save and it will correctly add the files to your db. Here's the snippet of code I used.

Disclaimer: I am not a PHP guru, nor a Drupal guru, so this may not be the best syntax. If you have suggestions for improvement, let me know.

				$node = new StdClass();
				// Information for base node
				$node->type = 'flexinode-1';  // *** This needs to be a valid content type
				$node->uid = $user->uid;
				
				$node->status = 1;  // Set to 0 if you do not want the content published
				$node->promote = 0;  // Set to 1 if you do not want the content promoted to the front page
				$node->sticky = 0;  // Set to 1 if you do not want the content sticky
				$node->title = 'title here';
				$node->body = 'content here';
				$node->teaser = 'content here';
				
				node_save($node);
				
				$upload = new StdClass();
				$upload->fid = 'upload';
				$upload->filename = $file;
				$upload->filepath = $dir.'/'.$file;
				$upload->filemime = 'application/pdf';
				$upload->filesize = filesize($dir.'/'.$file);
				$upload->description = $file;
				$upload->list = 1;
				$node->files = array();
				$node->files[0] = $upload;
				upload_save($node); 

----------------------------------------
Chuck Crandall
WebChuck Web Development
chuck at webchuckweb dot com
----------------------------------------