You are here

function pack_upload_media_upload_form_submit in Pack & Upload 7

Implements hook_form_submit().

File

./pack_upload.module, line 103
This is the module file for pack and upload.

Code

function pack_upload_media_upload_form_submit($form, $form_state) {
  $file = file_save_upload('file', array(
    'file_validate_extensions' => array(
      'zip tar tar.gz',
    ),
  ));

  // Created a directory if not available.
  if (!is_dir(variable_get('pack_upload_bulk_media_path'))) {
    drupal_mkdir(variable_get('pack_upload_bulk_media_path'), variable_get('file_chmod_directory', 0755));
  }

  // Creating a streamwrapper URI.
  $uri = variable_get('pack_upload_bulk_media_path');
  if ($file) {
    if ($file = file_move($file, $uri, FILE_EXISTS_RENAME)) {
      $form_state['values']['file'] = $file;
      $filename = $file->filename;
      $ext = pathinfo($filename, PATHINFO_EXTENSION);
      $realpath = drupal_realpath($file->uri);
      if ($ext == 'zip') {
        $zip = new ZipArchive();
        $zip
          ->open($realpath);
        $result = @$zip
          ->extractTo(drupal_realpath($uri));
        if ($result === TRUE) {
          drupal_set_message(t('All media have been extracted to %path', array(
            '%path' => drupal_realpath($uri),
          )));
        }
        else {
          watchdog('error', 'There is some problem related to extraction of the file. Please upload and try again.', array(), WATCHDOG_ERROR, NULL);
          drupal_set_message(t('There is some problem related to extraction of the file. Please upload and try again.'), 'error', FALSE);
        }
        $zip
          ->close();
      }
      else {
        try {
          $phar = new PharData($realpath);
          $phar
            ->extractTo(drupal_realpath($uri), null, true);

          // extract all files, and overwrite
          drupal_set_message(t('All media have been extracted to %path', array(
            '%path' => drupal_realpath($uri),
          )));
        } catch (Exception $e) {
          watchdog('error', 'There is some problem related to extraction of the file. Please upload and try again.', array(), WATCHDOG_ERROR, NULL);
          drupal_set_message(t('There is some problem related to extraction of the file. Please upload and try again.'), 'error', FALSE);
        }
      }
    }
    else {
      form_set_error('file', t("Failed to write the uploaded file the file folder."));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}