Last updated June 7, 2014. Created on June 27, 2009.
Edited by acabouet, silverwing, SpudEater. Log in to edit this page.

I made this snippet to simplify upload of thousands of images into 1,500 uc_product nodes.

The product content type contained 16 cck image fields.

The code looks to find a match for the image directory name with the SKU of a product. If it does - it picks up the jpg files within in alphabetical order and creates a file object for each CCK field then updates the node.

Then it moves the file from files/library/*productSKU*/ and into files/product_images/ and attempts to delete the directory.

The updating of the node automatically deletes the current set of attached images in product_images.

It's a total mess - I have NO idea how to make this into a module - but it works and all I have to do is view the page and the code runs!

I thought someone else might find it useful.


define ( 'BASEURL', '/home/*route to the files directory*/files' );


function findLibrary($Directory){
$MyDirectory = opendir($Directory) or die('Erreur');
	while($Entry = @readdir($MyDirectory)) {
		if($Entry != '.' && $Entry != '..'){
		if(is_dir($Directory.'/'.$Entry)) {
			$imageDirs[] = $Entry;
		}
                }
	}
  closedir($MyDirectory);
  return $imageDirs;
}

$libraries = findLibrary(BASEURL.'/library');

foreach($libraries AS $library){   // let's go find the NID
	if($productNid = get_me_my_nid($library)){
		print "$library found ($productNid)
"; if($newImages = scanLibrary($library)){ nodeUpdate($productNid,$newImages); }else{ print " but there are no images within "; if(rmdirr(BASEURL.'/library/'.$library)) print " so I removed it"; print "
"; } }else{ print "Unable to locate product $library. Please check the product SKU is the same as the image directory"; if(rmdirr(BASEURL.'/library/'.$library)) print " (I've got rid anyway)"; print "
"; } } function scanLibrary($Directory){ $MyDirectory = opendir(BASEURL.'/library/'.$Directory) or die('Erreur'); while($Entry = @readdir($MyDirectory)) { $jpg1 = strrpos($Entry, 'jpg'); $jpg2 = strrpos($Entry, 'JPG'); if($jpg1 || $jpg2){ // don't go here if it's a directory $newImage[$Entry] = array('id'=>$Entry,'filesize'=>filesize(BASEURL.'/library/'.$Directory.'/'.$Entry), 'filepath'=>'product_images/'.$Entry, 'orgpath'=>'library/'.$Directory.'/'.$Entry); } } closedir($MyDirectory); asort($newImage); return $newImage; } function get_me_my_nid($param = NULL) { $cond = 'model' ." = '%s'"; $arguments[] = $param; $fields = 'nid'; $node = db_fetch_object(db_query('SELECT '. $fields .' FROM {uc_products} WHERE '. $cond, $arguments)); return $node->nid; } function nodeUpdate($nid,$newImages){ $node = node_load($nid); $form_state = array(); module_load_include('inc', 'node', 'node.pages'); $node = node_load($nid); // load node # 31 $form_state['values']['op'] = t('Save'); // this seems to be a required value //clear out the old pictures; $iC = 1; foreach($newImages as $newImage){ $file = $newImage['filepath']; $name = basename($file); $file_obj = new stdClass(); $file_obj->filename = $name; $file_obj->filepath = "sites/default/files/".$newImage['filepath']; $file_obj->filemime = 'image/jpeg'; $file_obj->filesize = $newImage['filesize']; $file_obj->filesource = $name; $file_obj->uid = 1; $file_obj->status = 1; $file_obj->timestamp = time(); $file_obj->list = 1; $file_obj->uid = 1; $file_obj->status = 1; $file_obj->data = array(); $file_obj->timestamp = time(); $file_obj->new = true; drupal_write_record('files', $file_obj); $img = array( array( 'fid' => $file_obj->fid, 'filename' => $file_obj->filename, 'filepath' => $file_obj->filepath, 'filesize' => $file_obj->filesize, 'filemime' => $file_obj->filemime, 'timestamp' => $file_obj->timestamp, 'data' => array('title' => $file_obj->filename, 'description' => '', 'alt'=> ''), 'uid' => 1, 'list' => 1 ) ); if($iC <= 9){ $nodeName = 'field_image_0'.$iC; }else{ $nodeName = 'field_image_'.$iC; } $node->$nodeName = $img; $form_state['values'][$nodeName] = $img; $iC++; } for($iC; $iC<=16; $iC++){ // clear out the remaining images if($iC <= 9){ $nodeName = 'field_image_0'.$iC; }else{ $nodeName = 'field_image_'.$iC; } $node->$nodeName = array (0 => 0); } node_save($node); // now move the files into product_images - the originals should have been deleted previously foreach($newImages as $newImage){ $move_from_here = BASEURL.'/'.$newImage['orgpath']; $move_to_here = BASEURL.'/'.$newImage['filepath']; if (file_copy( $move_from_here , $move_to_here, FILE_EXISTS_REPLACE ) ){ //print $newImage['id']."
"; } } $imageBase = explode('/',$newImage['orgpath']); if(rmdirr(BASEURL.'/'.$imageBase[0].'/'.$imageBase[1])){ print $imageBase[1]." deleted
"; }else{ print $imageBase[1]." deletion failed (probably permissions problem)
"; } } /** * Delete a file, or a folder and its contents (recursive algorithm) * * @author Aidan Lister * @version 1.0.3 * @link http://aidanlister.com/repos/v/function.rmdirr.php * @param string $dirname Directory to delete * @return bool Returns TRUE on success, FALSE on failure */ function rmdirr($dirname) { // Sanity check if (!file_exists($dirname)) { return false; } // Simple delete for a file if (is_file($dirname) || is_link($dirname)) { return @unlink($dirname); } // Loop through the folder $dir = dir($dirname); while (false !== $entry = $dir->read()) { // Skip pointers if ($entry == '.' || $entry == '..') { continue; } // Recurse rmdirr($dirname . DIRECTORY_SEPARATOR . $entry); } // Clean up $dir->close(); return @rmdir($dirname); }

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

GreyHawk’s picture

This looks like it could be quite useful for migrating folks into a Drupal/Ubercart setup.

Has anything ever come of it...?