doubei5114 2014-11-29 11:03
浏览 64

从一个html表单上传不同格式和大小的多个文件

I have a html form with some text input fields and three file upload fields. two of them should be images and one should be a file (pdf). I am submitting this form using jquery ajax. My code is working well but I feel this is not the proper OO way to do this I feel I am repeating my self. So if someone can guide me through a proper way It will be a big help.

This is my HTML form

<form class="form-horizontal save-image-form" enctype="multipart/form-data" method="POST" action="<?php echo base_url() . 'loc_emp_reg_prc'; ?>">
                            <fieldset>
<!-- File Button --> 
                                <div class="form-group">
                                    <label class="col-md-4 control-label" for="loc_emp_idCpy">Employee ID Copy</label>
                                    <div class="col-md-4">
                                        <input id="loc_emp_idCpy" name="loc_emp_idCpy" class="input-file" type="file">
                                    </div>
                                </div>

                                <!-- File Button --> 
                                <div class="form-group">
                                    <label class="col-md-4 control-label" for="loc_emp_cntrct">Employee Contract</label>
                                    <div class="col-md-4">
                                        <input id="loc_emp_cntrct" name="loc_emp_cntrct" class="input-file" type="file">
                                    </div>
                                </div>
                                <!-- File Button --> 
                                <div class="form-group">
                                    <label class="col-md-4 control-label" for="loc_emp_pht">Employee Photo</label>
                                    <div class="col-md-4">
                                        <input id="loc_emp_pht" name="loc_emp_pht" class="input-file" type="file">
                                    </div>
                                </div>
</fieldset>
                        </form>

This is controller function

function loc_emp_reg_prc() {
        if ($this->form_validation->run('loc_emp_reg') == FALSE) {
            $validation_errors = validation_errors();
            echo ($validation_errors);
        } else {
            $salt = 'ophl';
            $lstEmpNbr = $this->home_basic_curd->select_ordered_and_limited('loc_emp_id', 'DESC', 'lco_emp_nmbr', 1, 'loc_emp');
            $newEmpNbr = $this->empNumberCreate($lstEmpNbr, $salt);
            $postArray = $this->input->post();
            $this->admin_image_manage->create_folder('uploads/' . $newEmpNbr);
            $uploadPath = 'uploads/' . $newEmpNbr . '/';
            $max_size = '3000';
            $max_width = '400';
            $max_height = '600';
            $types1 = 'gif|jpg|png|JPG|GIF|PNG|PDF|pdf';
            $types2 = 'PDF|pdf';
            $errorString = "";
            $is_upload = $this->admin_image_manage->upload_this_image($uploadPath, $max_size, $max_width, $max_height, 'loc_emp_idCpy', $types1);
            if ($is_upload['is_uploaded']) {
                $post_array['loc_emp_idCpy'] = $is_upload['rdata'];
                echo $post_array['loc_emp_idCpy'];
            } else {
                foreach ($is_upload['rdata'] as $error) {
                    $errorString = $error;
                }
            }
            if (!empty($_FILES['loc_emp_cntrct']['name'])) {
                $is_upload = $this->admin_image_manage->upload_document($uploadPath, $max_size, 'loc_emp_cntrct', $types2);
                if ($is_upload['is_uploaded']) {
                    $post_array['loc_emp_cntrct'] = $is_upload['rdata'];
                    echo $post_array['loc_emp_cntrct'];
                } else {
                    foreach ($is_upload['rdata'] as $error) {
                        $errorString = $error;
                    }
                }
            }
            $is_upload = $this->admin_image_manage->upload_this_image($uploadPath, $max_size, $max_width, $max_height, 'loc_emp_pht', $types1);
            if ($is_upload['is_uploaded']) {
                $post_array['loc_emp_pht'] = $is_upload['rdata'];
                echo $post_array['loc_emp_pht'];
            } else {
                foreach ($is_upload['rdata'] as $error) {
                    $errorString = $error;
                }
            }
            echo $errorString;
        }
    }

This is the models function 'admin_image_manage'

function upload_this_image($uploadPath, $max_size, $max_width, $max_height, $image, $types) {

        $config = array(
            'allowed_types' => $types, //only accept these file types
            'upload_path' => $uploadPath, //upload directory    
            'remove_spaces' => TRUE,
            'max_size' => $max_size,
            'max_width' => $max_width,
            'max_height' => $max_height,
        );

        $this->load->library('upload', $config);
        $upload_errors = "";
        if (!$this->upload->do_upload($image)) {
            $error = array('error' => $this->upload->display_errors());
            return array(
                'is_uploaded' => 0,
                'rdata' => $error
            );
        } else {
            $image_data = $this->upload->data();
            $image = $image_data['file_name'];
            return array(
                'is_uploaded' => 1,
                'rdata' => $image
            );
        }
    }

function create_folder($folderPath) {
        if (is_dir($folderPath)) {
            return FALSE;
        } else {
            mkdir($folderPath);
            return TRUE;
        }
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了