Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Here's a snippet for programmatically creating nodes with imagefields:
<?php
$node = new StdClass();
$node->type = 'official_photo';
$node->title = t('Sample official photo');
$node->taxonomy[4] = 15; // Candid shots gallery
$file_temp = file_get_contents('./profiles/multisite/files/official-photo.png');
$file_temp = file_save_data($file_temp, file_directory_path() .'/official_photos/official-photo.png', FILE_EXISTS_RENAME);
$node->field_photo = array(
array(
'fid' => 'upload',
'title' => basename($file_temp),
'filename' => basename($file_temp),
'filepath' => $file_temp,
'filesize' => filesize($file_temp),
),
);
$node->uid = 1;
$node->status = 1;
$node->active = 1;
$node->promote = 1;
node_save($node);
?>
You'll have to add the code for your other fields if any, obviously
Comments
Needed file id for this to work
I could not get this to work until I added
'fid' => 'upload' like this :
As far as I can tell, this makes sure the files table is populated correctly.
ImageField seems to drop a subdirectory
Our script, using the code above, sends this to node_save:
And yet, after node save, what's happened is this:
The image has been uploaded to files/images/ instead of files/images/61539/. AND(!) it's a duplicate (_aa_2.jpg). And yet, on occasion, it DOES keep the numbered subdirectory... seemingly without rhyme or reason. Appears to me that one of the functions drops the last subdirectory. Since other images (not attached to any node) go in there BEFORE we run node_submit/node_save, we know the directory is writable. (I also have it print the result of 'is_writable' first, and the directories are always writable.) Another thing I do is retrieve the fid if it exists first, and if it does, I send that instead of fid=>upload, but since I search by the file path the image SHOULD be in, it comes back empty for these messed-up-path images.
I'm hoping someone may have an idea how to fix this problem, or even what the problem is.
Programmatically creating nodes with ImageFields
Hi,
How can I change this to get it working in Drupal 6?
CCK upload fields and attachments
This custom node_save() function uploads CCK fields or files as attachments (besides handling changed value and returning the node object when done).
Usage:
Twitter: http://twitter.com/lx_barth
D6 code
Above code did not work for me. Issue #292904: Mass import/upload? may help you in Drupal 6.
FileField
To attach a file in a "File" field (i.e. the field type supplied by FileField module) you have to add a "description" item to the field array, like this:
Is this also supposed to
Is this also supposed to work in D6?
Drupal 6?
I too am having trouble getting this to work in Drupal 6. The node gets created, but the CCK image field is empty.
This is the code I'm using:
Did you get this working ?
I've been tearing my hair out for hours, but no luck.
Did you find a solution?
Same here
And no luck with David's post below either. =(
--
https://www.codeenigma.com/
Programmatically creating nodes with ImageFields drupal 6
Hi
what to change to get it working in drupal 6. I followed david's post but not working. anyhelp pls?
I have related response here:
I have related response here: http://drupal.org/node/458778#comment-1653696
This works for imagefield in Drupal 6
I uploaded the files to the dest in the files dir but I needed to associate them with the existing nodes.
Here's the code
hope it helps.
DT
Thanks David, very clean :)
Thanks David, very clean :)
Creating a node with the Image module
I'm stuck trying to create the necessary array for the image_node_form.
Does anyone know what the array should contain.
Programmatically creating nodes with ImageFields
How can I chnage this code to get it working in Drupal 6
Is it possible to insert a
Is it possible to insert a image title into each image node while using this snippet or something of the like? For example, along with the $file object, I would want a title and maybe alt text to be inserted with each object. Where would this information go?
Only works for insert node for me
When working on a node which already exists, this function is called:
filefield_field_update($node, $field, &$items, $teaser, $page)
This one will delete every old image associated with the node and which is not present in it's third parameter.
If it still doen't work, try
If it still doen't work, try adding the following piece of code:
(taken from the imagefield_import module)
Limits on image sizes
I tried just about every code example on this page and others that I could find. Nothing worked. Just silent failure.
Eventually I realized that the images I was uploading were larger than the maximum file size I had set, and the automatic scaling down of images which would occur if I uploaded images through a web form was not being done.
So far I don't know the incantation to get drupal to scale the images as it imports them, and I think I'm just going to do it from the command line with ImageMagick.
I've been using drush to run my import script in drupal context. I'm thinking that if I'd been working through a web interface some error messages might have been displayed. Maybe there's some call I need to make to get the error messages that need displaying.
Any pointers on these issues much apreciated.
Of course
Drupal always has to upload the image before it can act on it (e.g. process a resize), so if you set an image file size that is too restrictive then it will never get as far as resizing, because it will refuse the upload.
If your intention is to upload and resize a batch of images on the server, I would remove the file size restriction for the duration of the import and let Image Field do it's thing. Then reinstate the file size limit afterwards, if you are concerned users will upload silly-big files.
See ImageCache and ImageAPI modules for more powerful dynamic image manipulation options, including ImageMagick integration within Drupal.
Hope that helps! =)
--
https://www.codeenigma.com/
I know how to save node and I
I know how to save node and I need just to add image field to node before save it. I tried to use this part of code:
But every time when I use $_FILES['tmp_name']['image'] instead of "./profiles/multisite/files/official-photo.png" and $_FILES['name']['image'] instead of "/official_photos/official-photo.png" I have error "file_get_contents()... Filename cannot be empty". What I do wrong?
Do files need to sit under
Do files need to sit under sites/default/files...?
Also, did anyone success with Filefield Paths module and programmatic CCK Image upload? For me it is not putting it in the path I set as default.
My Drupal sites:
For D6
Check this out for D6
http://drupal.org/node/330421
My Drupal sites:
imagefield_import
you can use http://drupal.org/project/imagefield_import
D7 Version
Here's a D7 Version
Thanks for the snippit, I
Thanks for the snippit, I found that I needed to put a 'display' key set to 1 in the $node->field_images['und'][0] array, other than that, worked like a charm.
Also needed 'display' set to '1'
+1
Also needed 'display' set to '1' (or at least have it not be NULL)
Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.
Thanks for the D7 code!
I used it in a loop to do a mass import of a huge amount of images.
It really saved me a lot of time!
if you mean multiple upload
if you mean multiple upload please let me have the codes. thanks
Great, the D7 code works for me!
Great, the D7 code works for me! Lovely that the images get resized automatically...
Laurens Meurs
wiedes.nl
file_save_data
the d7 version works perfect for me IF the file is in the drupal root directory...however, when i move the file to my modules folder, it says that the temp file could not be copied...
$path = './path-to-uploaded-image/'.$id;
$filetitle = 'test';
$filename = $id;
$file_temp = file_get_contents($path);
$file_temp = file_save_data($file_temp, 'public://' . $filename,
FILE_EXISTS_RENAME);
i suspect it has something to do with the public:// reference...so my question, is how do i get a file that exists in my module folder copied back to sites/default/files??
ive tried just about everything path and permissions wise that i can think of....again, this works fine if the script is in my drupal root
thanks!!!
r
When updating in Drupal 7
I kept running into a problem when I would update an existing image field with my own rendered images, they'd get deleted off the file system as soon as I called node_save(). If I never called node_save(), it didn't get deleted.
What I had to do, was use the drupal function file_usage_add(). Check it out on drupal's API.
My code which works for D7, updating existing nodes:
Can anyone suggest the
Can anyone suggest the correct way of making this (or any) php/drupal code execute? I can make a mypage.php but if I just run this some of the drupal code won't execute. Is there a "proper" place to execute scripts from?
This is probably a very basic question and it seems like it should be easy to do. Making a new module seems a bit overkill but perhaps that is the way...?
You have to create a new,
You have to create a new, custom module. It's basically just a PHP file, renamed to .module, so it can contain whatever PHP code you want. But in order to establish a "path" or URL for a function to run on, you also have to tinker with the hook_menu function.
You should try to find a beginner's guide to module development in Drupal. It isn't necessarily hard or difficult, but it's more involved than creating a simple PHP script.
Richard
<?phpfile_usage_add($file,
Should it be $node->nid instead of $user->uid? Otherwise after the node has been deleted, the attached image will not be removed.
So it will be something like this:
Drupaler China
Good spot, thanks for taking
Good spot, thanks for taking the time to point this out.
Attaching image files to node.
Hi all,
It is quite easy to add a node programmatically in Drupal 7. But it gets little bit complex when we need to add an image to this node as well. Here is one example of this.you can try
http://findnerd.com/list/view/Attaching-image-files-to-nodes-programmati...