Multiple file upload/zip FAQ question

electricempire 09 Aug, 2013
on this FAQ: http://www.chronoengine.com/faqs/2993-how-can-i-zip-uploaded-files.html

at the bottom it says "By default the new zip file will be named after the first uploaded file found; edit the highlighted code to change this."

highlighted text is "$f['name']," - but when I change this text then the zip filename stays as first found file name. I commented the entire line out as well and did not receive errors
GreyHead 09 Aug, 2013
Hi electricempire,

There was a little typo in the FAQ, sorry, the comma at the end has to be kept. What are you replacing $f['name'] with?

Bob
electricempire 12 Aug, 2013
I replaced $f['name'] with 'docs'

this is what the line looks like
'name' => 'docs',
GreyHead 12 Aug, 2013
Hi electricempire,

That should be OK I think. Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here.

Bob
electricempire 12 Aug, 2013
Debug Data

    Upload routine started for file upload by : input_file_1
    /public_html/components/com_chronoforms/uploads/test/20130812160951_banner-ad.jpg has been uploaded successfully.
    Upload routine started for file upload by : input_file_3
    /public_html/components/com_chronoforms/uploads/test/20130812160951_Mercedes-Benz-used.jpg has been uploaded successfully.
    email
        0
            Result An email has been SENT successfully ...
            Body
            Label Text 	20130812160951_banner-ad.jpg
            Label Text 	20130812160951_Mercedes-Benz-used.jpg


            Submitted by 105.237.66.237
            Attachments array ( 0 => '/public_html/components/com_chronoforms/uploads/test/20130812160951_banner-ad.zip', )
GreyHead 13 Aug, 2013
Hi electricbanner,

That all looks good :-( I've reviewed the code in the FAQ and that looks OK. Please take a Form Backup using the icon in the Forms Manager and post it here (as a zipped file) or PM or email it to me and I'll try and debug the problem.

Bob
GreyHead 16 Aug, 2013
Hi electricempire,

Had to fiddle quite a lot to get the name change to work but it seems OK now.

<?php
// set the name for the zip file here
$zip_file_name = 'docs';

// check if there are any files to zip
if ( count($form->files) < 1 ) {
  return;
}
jimport('joomla.filesystem.file');
$files = array();
// loop through the files and unpack any arrays from the widget
foreach ( $form->files as $k => $f ) {
  if ( is_array($f) && !isset($f['name']) ) {
    foreach ($f as $kk => $ff ) {
      $files[$k.'_'.$kk] = $ff;
    }
  } else {
    $files[$k] = $f;
  }
}

$first_file = true;
foreach ( $files as $k => $f ) {
  if ( !JFile::exists($f['path']) ) {
    // check if the file exists
    continue;
  }
  if ( $first_file ) {
    // if it's the first file create the archive
        // if it's the first file create the archive
    if ( $zip_file_name ) {
      $zip_path = str_replace($f['name'], $zip_file_name.'.zip', $f['path']);
    } else {
      $zip_path = JFile::stripExt($f['path']).'.zip';
    }
    $zip = new ZipArchive;
    $result = $zip->open($zip_path, ZipArchive::OVERWRITE);
    if ( $result !== true ) {
      // unable to open ZipArchive
      return;
    }
    // create a new $form->files entry
    $form->files['upload_zip'] = array(
      'name' => 'docs',
      'original_name' => '',
      'path' => $zip_path,
      'size' => '',
      'link' => str_replace(JPATH_SITE.DS, JURI::root(), $zip_path)
    );
    $first_file = false;
  }
  // add the file to the archive
  $zip->addFile($f['path'], $f['name']);
  // uncomment the next line to delete the original file
//JFile::delete($f['path']);
}
$zip->close();
// update the size now the archive is complete
$form->files['upload_zip']['size'] = filesize($zip_path);
?>

Bob
This topic is locked and no more replies can be posted.