2011-10-07 6 views
0

모듈 구성 섹션에서 파일 업로드를 처리하려면 어떻게해야합니까? 여기까지 내가 지금까지 가지고있는 것이있다. 당신은 당신을 위해 대부분의 처리를 할 것 managed_file 유형 대신 드루팔을 사용하는 경우drupal 모듈 구성 프로세스 업로드 파일

<?php 
function dc_staff_directory_admin_settings() 
{ 
    $form['dc_staff_directory_upload_file'] = array(
    '#type' => 'file', 
    '#title' => t('Upload staff directory excel (.xls) file'), 
    '#description' => t('Uploading a file will replace the current staff directory'), 
); 
    $form['#submit'][] = 'dc_staff_directory_process_uploaded_file'; 
    return system_settings_form($form); 
} 

function dc_staff_directory_process_uploaded_file($form, &$form_state) 
{ 
    //What can I do here to get the file data? 
} 

답변

4

, 당신은 당신의 제출 기능에 영구 저장을위한 파일을 표시해야합니다

function dc_staff_directory_admin_settings() { 
    $form['dc_staff_directory_upload_file'] = array(
    '#type' => 'managed_file', 
    '#title' => t('Upload staff directory excel (.xls) file'), 
    '#description' => t('Uploading a file will replace the current staff directory'), 
    '#upload_location' => 'public://path/' 
); 

    $form['#submit'][] = 'dc_staff_directory_process_uploaded_file'; 
    $form['#validate'][] = 'dc_staff_directory_validate_uploaded_file'; 
    return system_settings_form($form); 
} 

function db_staff_directory_validate_uploaded_file($form, &$form_state) { 
    if (!isset($form_state['values']['dc_staff_directory_upload_file']) || !is_numeric($form_state['values']['dc_staff_directory_upload_file'])) { 
    form_set_error('dc_staff_directory_upload_file', t('Please select an file to upload.')); 
    } 
} 

function dc_staff_directory_process_uploaded_file($form, &$form_state) { 
    if ($form_state['values']['dc_staff_directory_upload_file'] != 0) { 
     // The new file's status is set to 0 or temporary and in order to ensure 
     // that the file is not removed after 6 hours we need to change it's status 
     // to 1. 
     $file = file_load($form_state['values']['dc_staff_directory_upload_file']); 
     $file->status = FILE_STATUS_PERMANENT; 
     file_save($file); 
    } 

} 

유효성 검증 기능입니다 아마도 좋은 아이디어 일 것입니다. 파일이 필수 필드가 아닌 경우에는 분명히 필요하지 않습니다.

이것은 주로 image_example 모듈에서 가져온 것으로 Examples Module의 일부입니다. managed_file 유형을 사용하고 싶지 않다면 같은 컬렉션에있는 file_example 모듈을 살펴보십시오. 관리되지 않는 파일을 업로드하는 방법에 대한 예제가 있습니다.

희망하는 사람