Jump to content

Add image to page API Adding duplicates


joer80
 Share

Recommended Posts

I have code that adds a remote image to a page that works great, but I want it to run and copy over the old image instead of adding it again and having both on the page.  Is there  a way to tell it to replace the old one?

This is what I have so far:  I first pass a | delimited string saved as $imageArray into this:

$m->setOutputFormatting(false);
 foreach($imageArray as $image){
	  if($image){
	    echo "Saving " . $image . " to " . $m->id . "<br>";	
	    $m->images->add($image);
	  }
 }
 $m->save();

Thanks!

Link to comment
Share on other sites

  • 3 years later...

Hi,

I tried to use the same technique and it's not working. I checked the image field to overwrite but the API keeps adding files, with "-1", "-2", "-3" appended to the filename.

Here's my code:

	foreach($files as $file) {
		if(substr($file, 0, 1) != '.' && $file != '.' && $file != '..') {

			// Get SKU
			$file_sku = substr($file, 0, 9);

			// Check for book
			$book = $pages->get('sku=' . $file_sku);
			if(!$book->id) {

				// Book doesn't exist

			}
			else {

				// Upload image into book
				$book->book_images->add($upload_directory . '/' . $file);

				// Delete file
				unlink($upload_directory . '/' . $file);

			}

			// Save book
			if($book->id) $book->save();

		}
	}

 

I don't know what's missing.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...