Jump to content
h365

Prevent Uploading Same Image Multiple Times

Recommended Posts

Hi guys, so i ran into an issue.

I have an Import Script for Products, each product has some images.

When i save them to Processwire the Images get uploaded. 

But when i run the importer again, the images are uploaded again, with filename-1, filename-2 etc..

Is there a way to prevent this.. so if an image already exists within a page i doesnt get uploaded again ? A Possibility would be to add the Source Url of the image as Metadata to the image and then check before uploading if it already exists or something?

Help is really appreciated! 

Thanks

Share this post


Link to post
Share on other sites

you can use count 

if (!count($page->images)) {
	// upload images
} else {
	// no upload
}

 

Share this post


Link to post
Share on other sites

but that wont check if the image already exists, and if not then upload it otherwise skip it?

Cheers

Share this post


Link to post
Share on other sites

For a page $p with images field "images" and an array of image paths/URLs...

$p->of(false);
$u = new WireUpload('image_check');
foreach($image_paths as $image_path) {
    // Get path parts
    $parts = pathinfo($image_path);
    // Convert filename to PW format
    $image_name = $u->validateFilename($parts['basename']);
    // Look for filename in images field
    if(!$p->images->get($image_name)) {
        // Image does not already exist in field, so add it
        $p->images->add($image_path);
    }
}
$p->save();

 

  • Like 3

Share this post


Link to post
Share on other sites

Thanks @Robin S that does the job! Do you know if the Wireupload module is capable of doing batch uploads if i provide multiple Image URLS? Or some kind of queueing? 

 

Share this post


Link to post
Share on other sites
8 minutes ago, h365 said:

Do you know if the Wireupload module is capable of doing batch uploads if i provide multiple Image URLS? Or some kind of queueing?

In this situation you aren't using WireUpload directly - rather Pageimages::add().

You can only give this method one image path at a time, but if you call it repeatedly in a loop it will keep uploading the images you give it until it is finished (or until PHP times out - you might want to increase max_execution_time if you have a lot of images to upload).

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...