2
\$\begingroup\$

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). 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>

I am submitting this form using jQuery AJAX:

$('.save-button').on('click', function(e) {
            e.preventDefault();
            var url = $(this).parents('.save-image-form').attr('action');
            var formData = new FormData($(this).parents('.save-image-form')[0]);

            $.ajax({
                url: url,
                type: 'POST',
                data: formData,
                async: false,
                success: function(data) {
                    alert(data)
                },
                cache: false,
                contentType: false,
                processData: false
            });
        });

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 modal '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;
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.