Hello,

I am trying to create new nodes with images using the imagefield and filefield module.

The code here under is able to create the new node but the image is not attached. When editing the node, I get an error message as follows:

    * warning: imagejpeg() [function.imagejpeg]: Unable to open 'sites/default/files/imagefield_thumbsc:/wamp/tmp/IMG_0068_0.jpg' for writing: Invalid argument in C:\wamp\www\drupaltest\includes\image.gd.inc on line 212.
    * An image thumbnail was not able to be created.
function checkdir3(){

$directory='sites/default/files';
$files=file_scan_directory($directory,'.jpg',array('.','..','cvs'),$callback,false);

foreach ($files as $file){
  $thefile=field_file_save_file($file->filename,array());
  $node = new stdClass();
  $node->type = 'stockimage';  
  $node->title = 'stockimage title new';
  $node->body = 'This is the body.';
  $node->teaser = 'This is the teaser.'; 
  $node->uid = 1;  
  $node->status = 1;
  $node->promote = 0;  
  $node->field_stimage = array($thefile);
  node_save($node); 
}
return;
}

Please note I am using WAMP on windows XP.

Thanks

Comments

systemick’s picture

I only ever use Linux so I'm not sure why this is happening on Windows but it seems to me that the directory in which the images are being stored isn't writable. You need to change the permissions on that directory

yoeld’s picture

I found the problem. See the modification in bold.

$thefile=field_file_save_file($file->filename,array(),$file->filename, FILE_EXISTS_REPLACE);

Looking at the API, you will see that the 3rd parameter is the destination of the file to be rewritten. By omitting that parameter, it was using a default destination that was not accessible.