Jump to content

add image zip from url


rick
 Share

Recommended Posts

I know I have to be missing something simple.

I need to specify via API a url to a zip file which contains images and have them saved in a back-end images field when the admin form is submitted. I have built a process module which generates a dynamic form and processes the form once submitted. Everything functions correctly except adding a zip archive of images to a multi-image field.

  • The images field works fine if the zip file is selected on a local machine.
  • The addimageurls module does not work with zip files.
  • The wirehttp->download will save the zip file to the specified folder, but the path returned is not accepted by the images field.
// $zip can be filespec or uri
$page->of(false);
$page->images->add($zip);
$page->save();

The images field has no file limit, zip extension added, and decompress zip is enabled.

Can someone provide the correct procedure please?

Link to comment
Share on other sites

I think you have to unzip it firstly and then add images to your images field

$zip = $config->paths->cache . "archive.zip";
$dst = $config->paths->cache . "temp-zip/";
$items = $files->unzip($zip, $dst);
$page->of(false);
if(count($items)) {
	foreach($items as $item) {
		$page->images->add($item);
	}
}
$page->save();

 

  • Like 1
Link to comment
Share on other sites

Thank you @Zeka. Your solution does work.

The base images field allows for a local zip file to be added via the Choose File option. I haven't found a means to accomplish this from the API.

For example, from the API:

  1. Download the desired file.zip file to a local folder. $this->config->paths->assets . "some-folder/" . basename( $url );
  2. Pass the file from that local folder path to the images field. $page->images->Add( "/var/www/test/site/assets/some-folder/file.zip" );
  3. Save the page.

Does not work. However, if I select Choose File and navigate to that same folder, select file.zip, and click Open, the zip file contents are added to the images field. So there is a difference I haven't yet been able to determine.

 

Link to comment
Share on other sites

15 hours ago, rick said:

So there is a difference I haven't yet been able to determine.

The unzipping behaviour is part of the inputfield itself not the PageFiles/PageImages class: https://github.com/processwire/processwire/blob/341342dc5b1c58012ae7cb26cffe2c57cd915552/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module#L943-L945 

  • Like 1
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...