我是一个新的编码。我正在开发一个学生应用系统。它需要上传图像和文件。现在,我正在尝试上传一个图像。我一直在寻找答案,但我没有找到一个可以解决我的问题。我已经调试了几个星期。图像的字段名是'image_input'。文件名进入数据库,但图像没有上传到文件夹。
我使用的是PHP版本8。我还添加了多部分,使文件夹下777权限和其他解决方案在这里,但没有工作到目前为止。我没有使用 AJAX 。
控制器:
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());
}
查看方式:
<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. name@gmail.com" 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(); ?>
产品型号:
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);
}
错误为:
Array([imageError] =〉您没有选择要上传的文件。
)
2条答案
按热度按时间frebpwbc1#
如果使用多部分表单类型发送图像并在控制器级别使用以下命令可以帮助解决问题,请参阅此命令
quhf5bfb2#
我已经解决了上传的问题。我在视图页面中添加了form_open,这就是为什么即使我有form_open_multipart也会出现错误的原因。无论如何,谢谢你的回答!