I am new to coding. I am developing a student application system. It requires uploading image and file. Now, I am trying to upload an image. I’ve been looking for answers here but I couldn’t find one that can solve my problem. I’ve been debugging this for weeks. The field name for the image is ‘image_input’. The file name enters the database but the image isn’t uploading in the folder.
I’m using PHP version 8. I also added the multipart, made the folder under 777 permission and other solutions here but nothing worked so far. I am not using AJAX.
Controller:
if($this->session->logged_in == true){
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">','</div>'); // optional if gusto mo ng css
$this->form_validation->set_rules('student_num', 'student number', 'required');
$this->form_validation->set_rules('first_name', 'first name', 'required');
$this->form_validation->set_rules('last_name', 'last name', 'required');
$this->form_validation->set_rules('course', 'course', 'required');
$this->form_validation->set_rules('year_level', 'year level', 'required');
$this->form_validation->set_rules('contact_num', 'contact number', 'required');
// $this->form_validation->set_rules('curriculum_eval', 'curriculum evaluation file', 'required');
// $this->form_validation->set_rules('award_applied', 'academic award', 'required');
$this->form_validation->set_rules('first_sem_gwa', 'first semester GWA', 'required');
$this->form_validation->set_rules('second_sem_gwa', 'second semester GWA', 'required');
$this->form_validation->set_rules('exception_reason', 'exception reason', 'not required');
// $this->form_validation->set_rules('image_input', 'image input', 'not required');
$image_input=$this->input->post('image_input');
$this->load->library('upload');
if($this->form_validation->run() == FALSE){
$page = "achievers";
if(!file_exists(APPPATH.'views/userdashboard/'.$page.'.php'))
{
show_404();
}
$data['title'] = "";
$this->load->view('templates/header');
$this->load->view('userdashboard/'.$page, $data);
$this->load->view('templates/footer');
}
else {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'pdf|jpg|png';
$image_input = "image_input";
$this->load->library("upload", $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($image_input))
{
$imageError = array('imageError' => $this->upload->display_errors());
print_r($imageError);
}
else
{
$file_name = $this->upload->data('file_name');
$data = array('image_input' => $file_name);
$this->Posts_model->insert_achievers();
$this->session->set_flashdata('achievers_added', 'Your application is received.');
redirect('user_home_page');
}
}
}
else {
redirect(base_url());
}
View:
<div class="col-md-8 .col-md-4 .col-lg-6">
<?php echo form_open_multipart('UserDashboard/achievers');?>
<form method="post" action="<?=base_url('UserDashboard/achievers') ?>" enctype="multipart/form-data" />
<div class="form-group row">
<label for="student_num" class="col-sm-2 col-form-label text-black ml-2"><b>Student Number</b></label>
<div class="col-sm-5">
<input type="text" name="student_num" class="form-control"
placeholder="e.g. 2019-00000-TG-0" id="student_num" value="<?= set_value('student_num'); ?>">
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="first_name" class="col-sm-2 col-form-label text-black ml-2"><b>First Name</b></label>
<div class="col-sm-7">
<input type="text" name="first_name" placeholder="Enter your first name" class="form-control" value="<?= set_value('first_name'); ?>">
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="last_name" class="col-sm-2 col-form-label text-black ml-2"><b>Last Name</b></label>
<div class="col-sm-7">
<input type="text" name="last_name" placeholder="Enter your last name" class="form-control" value="<?= set_value('last_name'); ?>">
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="email_address" class="col-sm-2 col-form-label text-black ml-2"><b>Email Address</b></label>
<div class="col-sm-7">
<input type="email" name="email_address" placeholder="e.g. [email protected]" class="form-control" value="<?= set_value('email_address'); ?>">
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="course" class="col-sm-2 col-form-label text-black ml-2"><b>Course</b></label>
<div class="col-sm-9">
<select type="course" name="course" class=" btn btn-primary dropdown-toggle" value="<?= set_value('course'); ?>" placeholder="Select Course" >
<option value="" class="dropdown-item">Select Course</option>
<option value="BSA" class="dropdown-item">BSA</option>
<option value="BSECE" class="dropdown-item">BSECE</option>
<option value="BSME" class="dropdown-item">BSME</option>
<span class="invalid-feedback"><?= $course_err ?></span>
</select>
</div>
</div>
<div class="form-group row">
<label for="year_level" class="col-sm-2 col-form-label text-black ml-2"><b>Year Level</b></label>
<div class="col-sm-9">
<select type="button" name="year_level" class="btn btn-primary dropdown-toggle" value="<?= set_value('year_level'); ?>" placeholder="Select Year Level">
<option value="" class="dropdown-item">Select Year Level</option>
<option value="first_year" class="dropdown-item">1st Year</option>
<span class="invalid-feedback" class="dropdown-item"></span>
</select>
</div>
</div>
<div class="form-group row">
<label for="contact_num" class="col-sm-2 col-form-label text-black ml-2"><b>Contact Number</b></label>
<div class="col-sm-4">
<input type="text" name="contact_num" placeholder="e.g. +639xxxxxxxxx" class="form-control " value="<?= set_value('contact_num'); ?>"></input>
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="curriculum_eval" class="col-sm-2 form-label text-black ml-2"><b>Curriculum Evaluation </b>(.pdf)</label>
<div class="col-sm-3">
<input type="file" name="curriculum_eval" class="" value="<?= set_value('curriculum_eval'); ?>"></input>
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="award_applied" class="col-sm-2 col-form-label text-black ml-2"><b>Award Applied</b></label>
<div class="col-sm-9 ">
<select type="course" name="award_applied" class=" btn btn-primary dropdown-toggle" value="<?= set_value('award_applied'); ?>"
placeholder="Select Course" >
<option value="" class="dropdown-item">Select Award</option>
<option value="achiever_award" class="dropdown-item">Achiever's Award</option>
<!-- <option value="academic_excellence" class="dropdown-item">Academic Excellence (4th/5th yr only)</option> -->
<span class="invalid-feedback"><?= $award_applied_err ?></span>
</select>
</div>
</div>
<div class="form-group row">
<label for="first_sem_gwa" class="col-sm-2 col-form-label text-black ml-2"><b>1st Sem GWA</b></label>
<div class="col-sm-3">
<input type="text" name="first_sem_gwa" placeholder="e.g. 1.00" class="form-control" value="<?= set_value('first_sem_gwa'); ?>"></input>
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<label for="second_sem_gwa" class="col-sm-2 col-form-label text-black ml-2"><b>2nd Sem GWA</b></label>
<div class="col-sm-3">
<input type="text" name="second_sem_gwa" placeholder="e.g. 1.00" class="form-control" value="<?= set_value('second_sem_gwa'); ?>"></input>
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<div class="d-sm-flex align-items-leftjustify-content-between col-lg-6 mb-5">
<label for="image_input" class=" col-form-label text-black ml-2"><b>Upload your 2x2 photo</b></label>
<div class="col-sm-8">
<input type="file" name="image_input" class="form-control" value="<?= set_value('image_input'); ?>"></input>
<div class="text-danger">
</div>
</div>
<span class="invalid-feedback"></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10 text-center">
<input type="submit" class="btn btn-primary " name="submit" value="Submit Application"></input>
<a href="<?php echo base_url() ?>user_home_page" class="btn btn-secondary text-white">Cancel</a>
</div>
</div>
</form>
<?php echo form_close(); ?>
Model:
public function insert_achievers()
{
$data = array(
'student_num' => $this->input->post('student_num'),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'slug' => url_title($this->input->post('student_num'), '-', true),
'email_address' => $this->input->post('email_address'),
'course' => $this->input->post('course'),
'year_level' => $this->input->post('year_level'),
'contact_num' => $this->input->post('contact_num'),
'curriculum_eval' => $this->input->post('curriculum_eval'),
'award_applied' => $this->input->post('award_applied'),
'first_sem_gwa' => $this->input->post('first_sem_gwa'),
'second_sem_gwa' => $this->input->post('second_sem_gwa'),
'status' => $this->input->post('status'),
'exception_reason' => $this->input->post('exception_reason'),
'image_input' => $this->input->post('image_input')
);
return $this->db->insert('student_applicants', $data);
}
The error is:
Array ( [imageError] =>
You did not select a file to upload.
)
2
Answers
I already solved the upload problem. I added the form_open in the view page that's why I got the error even though I have the form_open_multipart. Thanks for your answers anyway!
See this if it helps your problem
send the image using multipart form type
and at the controller level use the following