Home > Software design >  How to upload file into folder after saving form
How to upload file into folder after saving form

Time:11-30

I am trying to upload files through this form but it's only saving the name of the file into the database. I want that when the form submit then the file is uploaded to the destinated folder and the form submitted successfully, below I have provided both view and controller code

Form view code:

<form action='<?php echo base_url();?>careerform' method='POST' accept-charset='UTF-8' enctype="multipart/form-data">
            
            <div class="form-row">
                <div class="col-md-8 col-12 px-3">
                    <h3>Please fill in the details  :</h3>

                    <div class="form-row">
                        <div class="col-md-6 mb-3">
                            <div class="form-group">
                                <div class="has-float-label">
                                    <div class='zcwf_row'>
                                        <div class='zcwf_col_lab'>
                                            <label for='Full_Name'>First Name <span style='color:red;'>*</span></label>
                                        </div>
                                        <div class='zcwf_col_fld'>
                                            <input type='text' id='firstname' name='firstname' class="form-control" maxlength='80' required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6 mb-3">
                            <div class="form-group">
                                <div class="has-float-label">
                                    <div class='zcwf_row'>
                                        <div class='zcwf_col_lab'>
                                            <label for='Full_Name'>Last Name <span style='color:red;'>*</span></label>
                                        </div>
                                        <div class='zcwf_col_fld'>
                                            <input type='text' id='firstname' name='lastname' class="form-control" maxlength='80' required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="col-md-6 mb-3">
                            <div class="form-group">
                                <div class="has-float-label">
                                    <div class='zcwf_row'>
                                        <div class='zcwf_col_lab'>
                                            <label for='Mobile'>Mobile No. <span style='color:red;'>*</span></label>
                                        </div>
                                        <div class='zcwf_col_fld'>
                                            <input type='tel' id='phone' class="form-control" name='phone' maxlength='10' minlength='10' required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    
                        <div class="col-md-6 mb-3">
                            <div class="form-group">
                                <div class="has-float-label">
                                    <div class='zcwf_row'>
                                        <div class='zcwf_col_lab'>
                                            <label for='Email'>Email <span style='color:red;'>*</span></label>
                                        </div>
                                        <div class='zcwf_col_fld'>
                                            <input type='email' ftype='email' id='email' name='email' class="form-control" maxlength='100' required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="col-md-6 mb-3">
                            <div class="form-group">
                                <span class="has-float-label">
                                    <label for="name">Department<span style="color:red;">*</span></label>
                                    <select class="custom-select mr-sm-2" id="LEADCF23" name="department" required="">
                                        <option value="">- Select Department -</option>
                                        <option value="Sales">Sales</option>
                                        <option value="Sales Support">Sales Support</option>
                                        <option value="Finance & Accounts">Finance & Accounts</option>
                                        <option value="Human Resources">Human Resources</option>
                                        <option value="Information Technology">Information Technology</option>
                                        <option value="Marketing">Marketing</option>
                                        <option value="Operations">Operations</option>
                                        <option value="Customer Relations">Customer Relations</option>
                                        <option value="Purchase & SCM">Purchase & SCM</option>
                                        <option value="Other">Other</option>                                    
                                    </select>
                                </span>
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <div class="form-group">
                                <div class="has-float-label">
                                    <div class='zcwf_row'>
                                        <div class='zcwf_col_lab'>
                                            <label for='Full_Name'>Upload Resume <span style='color:red;'>*</span></label>
                                        </div>
                                        <div class='zcwf_col_fld'>
                                            <input class="form-control" type="file" accept="application/pdf" id="example-file-input" name="resume" require>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>            
           
                    <div class="form-row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <span class="has-float-label">
                                    <label for="text">Message</label>
                                    <textarea rows="4" name="comment" placeholder="" style="width:100%; border-radius:5px; height:auto" id="car-enquiry-comment"></textarea>
                                </span>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input type="checkbox" id="defaultCheck" name="example2" required="">
                            <label for="defaultCheck" class="declaimer">I Agree to the Privacy Policy and Terms of Service.</label>
                            <p>Disclaimer: I agree that by clicking the ‘Submit’ button below, I am explicitly soliciting a call / Message from Competent Automobiles Co. Ltd or its Representatives on my ‘Mobile’.</p>
                        </div>
                        <div class="col-md-12  mb-3">
                            <input type="hidden" id="zc_gad" name="zc_gad" value="" />
                            <button class="btn btn-primary formsubmit zcwf_button" id='formsubmit' name="submit" type="submit">Submit </button>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-12 px-3">
                    <img src="<?php echo base_url();?>assets/images/career.jpg" alt="service-appointment-bg" style="width: 100%;">
                </div>

            </div>
        </form>

Form controller code:

public function savecareer(){

    $this->load->config('email');
    $this->load->library('email');
        
    $from = '-';
    $to = $this->input->post('email');
    $subject = 'Contact Us';
    $firstname = $this->input->post('firstname');
    $lastname = $this->input->post('lastname');
    $phone = $this->input->post('phone');
    $subject = $this->input->post('subject');
    $comment = $this->input->post('comment');

    $message = 'FirstName : '.$firstname.'<br> LastName : '.$lastname.'<br> Phone : '.$phone.' <br> Subject : '.$subject.'<br> Comment : '.$comment;

        $this->email->set_newline("\r\n");
        $this->email->from($to);
        $this->email->to($from);
        $this->email->subject($subject);
        $this->email->message($message);

        $this->email->send();

    $data = array(  
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'phone' => $this->input->post('phone'),
            'email' => $this->input->post('email'),
            'department' => $this->input->post('department'),
            'resume'   => $this->input->post('resume'),
            'comment'   => $this->input->post('comment')
                     );  
        //insert data into database table.  

        $this->db->insert('tp_career',$data);  
        $this->session->set_flashdata('success', 'Test Drive Booking Successfully.');
        redirect("thankyou-career");  

    }

CodePudding user response:

public
function savecareer()
{
    $this->load->config('email');
    $this->load->library('email');

    $from = '-';
    $to = $this->input->post('email');
    $subject = 'Contact Us';
    $firstname = $this->input->post('firstname');
    $lastname = $this->input->post('lastname');
    $phone = $this->input->post('phone');
    $subject = $this->input->post('subject');
    $comment = $this->input->post('comment');

    $message = 'FirstName : ' . $firstname . '<br> LastName : ' . $lastname . '<br> Phone : ' . $phone . ' <br> Subject : ' . $subject . '<br> Comment : ' . $comment;

    $this->email->set_newline("\r\n");
    $this->email->from($to);
    $this->email->to($from);
    $this->email->subject($subject);
    $this->email->message($message);

    $this->email->send();

    /* FILE UPLOAD CODE */
    $target_dir = "YOUR_FOLDER_NAME/";
    $target_file = $target_dir . basename($_FILES["resume"]["name"]);
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    move_uploaded_file($_FILES["resume"]["tmp_name"], $target_file);
    /* FILE UPLOAD CODE */
    
    
        $data = array(
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'phone' => $this->input->post('phone'),
            'email' => $this->input->post('email'),
            'department' => $this->input->post('department'),
            'resume' => $target_file,
            'comment' => $this->input->post('comment')
        );
            //insert data into database table.
    
            $this->db->insert('tp_career', $data);
            $this->session->set_flashdata('success', 'Test Drive Booking Successfully.');
            redirect("thankyou-career");
    }

CodePudding user response:

After $this->db->insert('tp_career',$data); just write these lines of code:

$filename = "Resume_NameYouWant".'.'.pathinfo($_FILES["resume"]["name"], PATHINFO_EXTENSION); //creating file name
$tempname = $_FILES["resume"]["tmp_name"]; //after form submit it storest in temp location
$folderpath = "uploads/YourFolder"; //destination of your upload
$target_file = $folderpath .$filename ; //full path filename
    
if (move_uploaded_file($tempname, $target_file))  { //actually moves from temp to destination
    log_message('debug', "Resume file uploaded successfully: ".$target_file);
}else{
    log_message('debug', "Failed to upload Resume: ".$target_file);
}

NB: Based on your configuration these are some limitation of POST size, if not able to upload large file try uploading small file.

  • Related