Allow uploading files using single ZIP file

Started by 3ginfozone, June 24, 2006, 02:54:02 AM

Previous topic - Next topic

3ginfozone

I just started to use SMF and find it very much better than phpBB! Thanks to the SMF team  :D

Was looking for a way to quickly upload bunch of images and there are no ready made MODs for it, so I edited the SMF code a littlebit to automatically unzip any uploaded ZIP files and attach each of the files inside the ZIP.

In short, i duplicate the single attachment part and created a loop through each unzip-ed files.
Here's how i did it (Post.php, function Post2, SMF1.1RC2)




Original:
$attachmentOptions = array(
      'post' => isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 0,
      'poster' => $ID_MEMBER,
      'name' => $_FILES['attachment']['name'][$n],
      'tmp_name' => $_FILES['attachment']['tmp_name'][$n],
      'size' => $_FILES['attachment']['size'][$n],
   );


replace with:


require_once("Subs-Package.php");

// automatically unzip attachment that came in a .ZIP or .GZ
if (preg_match("/\.(gz|zip)$/", basename(stripslashes($_FILES['attachment']['name'][$n]))))
{
   // uncompress
   $extracted_files = read_tgz_file($_FILES['attachment']['tmp_name'][$n], $modSettings['attachmentUploadDir']);

   foreach ($extracted_files as $file)
   {
       // rename to tempname that matches the requirement of SMF
       $tempname = 'post_tmp_' . $ID_MEMBER . '_' . ($tmpname_counter++);
      rename($modSettings['attachmentUploadDir']."/".$file['filename'], $modSettings['attachmentUploadDir']."/".$tempname);
      
      $attachmentOptions = array(
         'post' => isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 0,
         'poster' => $ID_MEMBER,
         'name' => $file['filename'],
         'tmp_name' => $tempname,
         'size' => filesize($tempname),
      );

      if (createAttachment($attachmentOptions))
      {
         $attachIDs[] = $attachmentOptions['id'];
         if (!empty($attachmentOptions['thumb']))
            $attachIDs[] = $attachmentOptions['thumb'];
      }
      else
      {
         if (in_array('could_not_upload', $attachmentOptions['errors']))
            fatal_lang_error('smf124');
         if (in_array('too_large', $attachmentOptions['errors']))
            fatal_lang_error('smf122', false, array($modSettings['attachmentSizeLimit']));
         if (in_array('bad_extension', $attachmentOptions['errors']))
            fatal_error($attachmentOptions['name'] . '.<br />' . $txt['smf123'] . ' ' . $modSettings['attachmentExtensions'] . '.', false);
         if (in_array('directory_full', $attachmentOptions['errors']))
            fatal_lang_error('smf126');
         if (in_array('bad_filename', $attachmentOptions['errors']))
            fatal_error(basename($attachmentOptions['name']) . '.<br />' . $txt['smf130b'] . '.');
         if (in_array('taken_filename', $attachmentOptions['errors']))
            fatal_lang_error('smf125');
      }
   }
}
else
{
   $attachmentOptions = array(
      'post' => isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 0,
      'poster' => $ID_MEMBER,
      'name' => $_FILES['attachment']['name'][$n],
      'tmp_name' => $_FILES['attachment']['tmp_name'][$n],
      'size' => $_FILES['attachment']['size'][$n],
   );

   if (createAttachment($attachmentOptions))
   {
      $attachIDs[] = $attachmentOptions['id'];
      if (!empty($attachmentOptions['thumb']))
         $attachIDs[] = $attachmentOptions['thumb'];
   }
   else
   {
      if (in_array('could_not_upload', $attachmentOptions['errors']))
         fatal_lang_error('smf124');
      if (in_array('too_large', $attachmentOptions['errors']))
         fatal_lang_error('smf122', false, array($modSettings['attachmentSizeLimit']));
      if (in_array('bad_extension', $attachmentOptions['errors']))
         fatal_error($attachmentOptions['name'] . '.<br />' . $txt['smf123'] . ' ' . $modSettings['attachmentExtensions'] . '.', false);
      if (in_array('directory_full', $attachmentOptions['errors']))
         fatal_lang_error('smf126');
      if (in_array('bad_filename', $attachmentOptions['errors']))
         fatal_error(basename($attachmentOptions['name']) . '.<br />' . $txt['smf130b'] . '.');
      if (in_array('taken_filename', $attachmentOptions['errors']))
         fatal_lang_error('smf125');
   }
}



----
hxxp:www.3ginfozone.com [nonactive]

Advertisement: