2016-07-27 19 views
0

Ich implementiere eine Website, die Archive mit Verzeichnisstruktur in einer bestimmten Weise akzeptiert. Ich möchte die Verzeichnisstruktur in der ZIP-Datei prüfen, bevor ich sie akzeptiere. Ich habe versucht, die folgenden (siehe Kommentare inline):In Drupal, wie validiert man den Inhalt einer Datei hochgeladen mit Webforms und * nicht * upload, wenn es die Validierung fehlschlägt?

<?php 

Mit Webform Validierung:

// using the webform validation module and its hooks 
function testsuite_ziptest_webform_validation_validators() 
{ 
    return array(
     "validate_zip_file"=> array(
      'name' => "testsuite: Validate Zipfile" , 
      'component_types' => array(
       'select', 
       'file', 
      ), 
      'description' => t('Verifies that the contents of the zipfile adhere to the specifications of testsuite.'), 
     ) 
    ); 
} 

function testsuite_ziptest_webform_validation_validate($validator_name, $items, $components, $rule) 
{ 
    $errors = array(); 

    if($items) 
    { 
     switch($validator_name) 
     { 
     case "validate_zip_file":  
      drupal_set_message(t('Validate function called')); 
      foreach($items as $key=>$value) 
      { 
       drupal_set_message($key); 
       $v = _webform_validation_flatten_array($value); 
       drupal_set_message($v); 
      } 

      // tried to get the $fid and access the file using the fid. 
      // item_6 is the key of the file field that I selected while 
      // enabling webform validation. 
      // This fails saying no such file exists when the ziparchive 
      // object tries to open it. 

      $fid = $items['item_6']; 

      if(!empty($fid)) 
      { 
       $za = new ZipArchive(); 
       $file = file_load($fid); 
       $za->open($file->uri); 
       for($i = 0; $i < $za->numFiles; $i++) 
       { 
        $stat = $za->statIndex($i); 
        drupal_set_message($stat['name']); 
       } 
       $za->close(); 
      } 
      break; 
     } 
    } 

    return $errors; 
} 

hook_file_validate Mit

// this works, but there might be other files that may 
// be uploaded to the site and I only want it to trigger 
// when the file is uploaded as a part of a webform, not 
// for all file uploads. 
function testsuite_ziptest_file_validate($file) 
{ 
    if(!empty($file->filename)) 
    { 
     $za = new ZipArchive(); 
     $za->open($file->uri); 
     for($i = 0; $i < $za->numFiles; $i++) 
     { 
      $stat = $za->statIndex($i); 
      drupal_set_message($stat['name']); 
     } 
     $za->close(); 
    } 
} 

Forms API verwenden

// The following two methods that uses the form api on the webform 
// has the same issue as the webform validation module. I can't get 
// any reference to the file. 
// There is a reference through a "completed form" key but I don't know 
// if this is best practice 
// die statements were used for debugging 
function testsuite_ziptest_form_alter(&$form, &$form_state, $form_id) 
{ 
    if($form_id == 'webform_client_form_1') 
    { 
     array_unshift($form['#validate'], 'testsuite_ziptest_form_validate'); 
     return $form; 
    } 
} 

function testsuite_ziptest_form_validate($form, &$form_state) 
{ 
    echo '<pre>'; print_r($form_state); echo '</pre>'; 
    die(); 
    $fid = $form_state['values']['submitted']['attachment']; 
    if(!empty($fid)) 
    { 
     $za = new ZipArchive(); 
     $file = file_load($fid); 
     $za->open($file->uri); 
     for($i = 0; $i < $za->numFiles; $i++) 
     { 
      $stat = $za->statIndex($i); 
      drupal_set_message($stat['name']); 
     } 
     $za->close(); 
    } 
    else 
    { 

    } 
    die(); 
    return; 
} 
(?)

Danke!

Antwort

0

Ich denke, Sie verpassen einen Punkt, wenn Sie Ihre Funktion erledigt haben. Sie haben einfach vergessen, den Fehler zurückzusenden.

Im Webforms-Validierungsprozess müssen Sie einige Elemente im $ errors-Array zurücksenden, wenn etwas schief gelaufen ist.

$errors[$key] = t('%item is not good', array('%item' => $components[$key]['name'])); 

Here is an example der Verwendung dieser Methode.

In der Drupal Formularvalidierung müssen Sie form_set_error verwenden, wenn etwas nicht gut ist, mit dem Combonamen und der Fehlermeldung. Die Validierung stoppt dann automatisch und das Formular wird nicht gesendet ...

Und in der Methode hook_file_validate müssen Sie auch ein Array von Fehlern zurücksenden, die vom Validator verwendet werden, um die Übermittlung zu stoppen (mit form_set_error).

0

Seine Arbeitsbeispiel:

function yourmoduleNma_file_validate($file,$validators) 
{ 
    $errors = array(); 
    $filename = $file->filename; 
    $isValid_Extention_Size = explode('.',$filename);    
    if(count($isValid_Extention_Size) > 2){   
    $errors[] = 'Extention is not valid of this file'; 
    } 
    elseif($file->filesize <= 0) 
    {   
     $errors[] = 'File size should be greater than 0 Bytes and less than 5 MB'; 
    } 
    return $errors; 
}