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 >','</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 >
<?php echo form_open_multipart('UserDashboard/achievers');?>
<form method="post" action="<?=base_url('UserDashboard/achievers') ?>" enctype="multipart/form-data" />
<div >
<label for="student_num" ><b>Student Number</b></label>
<div >
<input type="text" name="student_num"
placeholder="e.g. 2019-00000-TG-0" id="student_num" value="<?= set_value('student_num'); ?>">
<span ></span>
</div>
</div>
<div >
<label for="first_name" ><b>First Name</b></label>
<div >
<input type="text" name="first_name" placeholder="Enter your first name" value="<?= set_value('first_name'); ?>">
<span ></span>
</div>
</div>
<div >
<label for="last_name" ><b>Last Name</b></label>
<div >
<input type="text" name="last_name" placeholder="Enter your last name" value="<?= set_value('last_name'); ?>">
<span ></span>
</div>
</div>
<div >
<label for="email_address" ><b>Email Address</b></label>
<div >
<input type="email" name="email_address" placeholder="e.g. [email protected]" value="<?= set_value('email_address'); ?>">
<span ></span>
</div>
</div>
<div >
<label for="course" ><b>Course</b></label>
<div >
<select type="course" name="course" value="<?= set_value('course'); ?>" placeholder="Select Course" >
<option value="" >Select Course</option>
<option value="BSA" >BSA</option>
<option value="BSECE" >BSECE</option>
<option value="BSME" >BSME</option>
<span ><?= $course_err ?></span>
</select>
</div>
</div>
<div >
<label for="year_level" ><b>Year Level</b></label>
<div >
<select type="button" name="year_level" value="<?= set_value('year_level'); ?>" placeholder="Select Year Level">
<option value="" >Select Year Level</option>
<option value="first_year" >1st Year</option>
<span ></span>
</select>
</div>
</div>
<div >
<label for="contact_num" ><b>Contact Number</b></label>
<div >
<input type="text" name="contact_num" placeholder="e.g. 639xxxxxxxxx" value="<?= set_value('contact_num'); ?>"></input>
<span ></span>
</div>
</div>
<div >
<label for="curriculum_eval" ><b>Curriculum Evaluation </b>(.pdf)</label>
<div >
<input type="file" name="curriculum_eval" value="<?= set_value('curriculum_eval'); ?>"></input>
<span ></span>
</div>
</div>
<div >
<label for="award_applied" ><b>Award Applied</b></label>
<div >
<select type="course" name="award_applied" value="<?= set_value('award_applied'); ?>"
placeholder="Select Course" >
<option value="" >Select Award</option>
<option value="achiever_award" >Achiever's Award</option>
<!-- <option value="academic_excellence" >Academic Excellence (4th/5th yr only)</option> -->
<span ><?= $award_applied_err ?></span>
</select>
</div>
</div>
<div >
<label for="first_sem_gwa" ><b>1st Sem GWA</b></label>
<div >
<input type="text" name="first_sem_gwa" placeholder="e.g. 1.00" value="<?= set_value('first_sem_gwa'); ?>"></input>
<span ></span>
</div>
</div>
<div >
<label for="second_sem_gwa" ><b>2nd Sem GWA</b></label>
<div >
<input type="text" name="second_sem_gwa" placeholder="e.g. 1.00" value="<?= set_value('second_sem_gwa'); ?>"></input>
<span ></span>
</div>
</div>
<div >
<div >
<label for="image_input" ><b>Upload your 2x2 photo</b></label>
<div >
<input type="file" name="image_input" value="<?= set_value('image_input'); ?>"></input>
<div >
</div>
</div>
<span ></span>
</div>
</div>
<div >
<div >
<input type="submit" name="submit" value="Submit Application"></input>
<a href="<?php echo base_url() ?>user_home_page" >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.
)
CodePudding user response:
See this if it helps your problem send the image using multipart form type and at the controller level use the following
if ($this->input->method(true) == 'POST') {
if (isset($_FILES['image'])) {
$config['upload_path'] = './public/uploads/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = 2048;
$config['file_name'] = time() . $_FILES['image']['name'];
$config['file_name'] = $this->security->sanitize_filename($config['file_name']);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image')) {
return $this->output
->set_content_type('application/json')
->set_status_header(200)
->set_output(json_encode(
array(
'status' => false,
'data' => [],
'error' => $this->upload->display_errors()
)
));
}
$base = base_url('/public/uploads/');
$file_name = $base . $this->upload->data('file_name');}}
CodePudding user response:
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!