Jump to content

problem with identical named images and wireZipFile


simonGG
 Share

Recommended Posts

Hi there,

i want to realize a ZIP download with pageImages but if 2 images have the same name only one is saved in the ZIP.
So i tried to rename the images like this:

foreach ($sweepstake_children as $sweepstake_member) 
{
  $member_image   = $sweepstake_member->images->first();
  $ext            = $member_image->ext;
  $newFilename    = $sweepstake_member->vorname . "-" . $sweepstake_member->nachname . "-" . $member_image->name. "." . $ext;
  $member_image->rename($newFilename);
}

But then i get an error: Call to a member function rename() on boolean ? Or ist this the wrong approch anayway?

best regards
Simon

Link to comment
Share on other sites

Do you use ZipArchive to build the zipfile? The method addFile may take more than a single argument:

https://www.php.net/manual/en/ziparchive.addfile.php

The first is the full path to the real, uncompressed file on disk, the (optional) second parameter is the name inside the zipfile. I'd simply specify something unique for the second argument then.

Edit: Ok, just realized you're referring to wireZipFile so probably not helpful.

Link to comment
Share on other sites

10 hours ago, simonGG said:

But then i get an error: Call to a member function rename() on boolean ?

Probably one of the $sweepstake_children doesn't have any images.

10 hours ago, simonGG said:

So i tried to rename the images

Probably best not to rename the images within the page because that results in the loss of potentially useful filename data and would break any links to the images within CKEditor fields.

Instead you could copy the files to a temporary directory, renaming them in the process, then create the zip and download from there.

Here is some code you can adapt for your purpose:

// Create temp directory
$td = $files->tempDir('to-zip');
$td_path = (string) $td;
$files_to_zip = array();
foreach($page->children() as $child) {
	$image = $child->images->first();
    // Continue if no image exists
    if(!$image) continue;
	// Set path including new filename as needed
	$new_path = $td_path . "{$child->id}.{$image->ext}";
	// Copy image to new path
	$success = $files->copy($image->filename, $new_path);
	// Add new path to array
	if($success) $files_to_zip[] = $new_path;
}
// Set destination path for zip file
$zip_path = $td_path . time() . '.zip';
// Create zip file
$success = $files->zip($zip_path, $files_to_zip);
// Send download
if($success) $files->send($zip_path);
// Optionally remove temp directory now rather than waiting for it to automatically expire
$td->remove();

 

  • Like 4
Link to comment
Share on other sites

4 hours ago, Hurlbert said:

Is this code applicable no matter how many images you have?

In theory yes, but in practice you will hit PHP memory and timeout limits at some point. Just try it and see how you get on.

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