檔案上傳 managed_file 多檔案

1 篇文章 / 0 new
author
檔案上傳 managed_file 多檔案
function multifiles_example_form($form, &$form_state) {
    // in case of ajax, form_state is already populated, as drupal_process_form already run before this.
    $form['allfiles'] = array(
        '#type' => 'container',
        '#tree' => true,
    );
    $filecount = 0;
    // first loading
    if (!$fileslist = variable_get('multifiles_example_files', FALSE)) {
        $fileslist = array('files' => array(), 'count' => 0);
    }
    // Use the #managed_file FAPI element to upload an image file.
    foreach ($fileslist['files'] as $key => $value) {//
        $form['allfiles']['file_' . $filecount] = array(
            '#title' => t('Image ' . $filecount),
            '#type' => 'managed_file',
            '#upload_location' => 'public://',
            '#default_value' => $value,
            '#upload_validators' => array(
                'file_validate_extensions' => array('gif png jpg jpeg'),
                'file_validate_size' => array(1000000),// 許可的最大檔案(byte)
            ),
        );
        $filecount++;
    }
    // 加入新的 upload item
    $form['allfiles']['file_' . $filecount] = array(
        '#title' => t('Image ' . $filecount),
        '#type' => 'managed_file',
        '#upload_location' => 'public://',
        '#upload_validators' => array(
            'file_validate_extensions' => array('gif png jpg jpeg'),
            'file_validate_size' => array(1000000),
        ),
        '#process' => array('import_my_file_element_process'),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
    );
    return $form;
}
 //呼叫時元素基本處理尚未完成
function import_my_file_element_process($element, &$form_state, $form) {
    $element = file_managed_file_process($element, $form_state, $form);
    $element['upload_button']['#access'] = FALSE;//加入 隱藏上傳按鈕 參數
    return $element;
}
function multifiles_example_form_submit($form, &$form_state) {
    // to get the current list
    if ($fileslist = variable_get('multifiles_example_files', FALSE)) {
        $i = 0;
        foreach ($fileslist['files'] as $fileindex => $fileidvalue) {
            if ($form_state['values']['allfiles']['file_' . $i] == 0) {//已被標記為刪除
                $file = file_load($fileidvalue);
                if ($file) {
                    file_usage_delete($file, 'multifiles_example', 'sample_image', 1);//刪除使用紀錄
                    file_delete($file);//移除紀錄, 若該檔已無使用紀錄則真正刪除檔案
                }
            }
            $i++;
        }
    }
    // now save files
    $newlist = array('files' => array(), 'count' => 0);
    foreach ($form_state['values']['allfiles'] as $key => $value) {
        if ($value != 0) {// file exists
            $file = file_load($value);//載入檔案資訊
            if ($file->status == 0) {//0: 檔案上傳後初始值為 0, 一般表示為暫時性檔案
                $file->status = FILE_STATUS_PERMANENT;//1 註記為永久
                file_save($file);
                file_usage_add($file, 'multifiles_example', 'sample_image', 1);//登陸使用紀錄
            }
            $newlist['files'][$file->fid] = $file->fid;
            $newlist['count'] = $newlist['count'] + 1;
        }
    }
    if ($newlist['count'] > 0) {
        variable_set('multifiles_example_files', $newlist);
    } else {
        variable_set('multifiles_example_files', false);
    }
    $form_state['redirect'] = 'multifiles_example';
    //$form_state['rebuild'] = TRUE;
}
Free Web Hosting