Jump to content

Prevent Uploading Same Image Multiple Times


h365
 Share

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

Link to comment
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
Link to comment
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).

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...