'Can not upload image into the database in codeigniter
I'm trying to upload a image to the database in codeigniter , but when I write
$this->load->model('Upload_model');$id = $this->Upload_model->add_image($data);,
the function doesn't work . I have add this model to the /config/autload
. But the controller still can not load the Upload_model
.The following is my code
The controller
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Main extends CI_Controller
{
public function _construct()
{
parent::_construct();
$this->load->library('form_validation'); //load form validation when object of this class has been made.
$this->load->library('encrypt'); // load and encrypt library when object of this class has been created.
$this->load->library('database');
$this->load->model('Upload_model');
}
//functions
function image_upload()
{
$data['title'] = "Upload Upload your course";
$this->load->view('template/header');
$this->load->view('Upload/image_upload', $data);
}
function ajax_upload()
{
if (isset($_FILES["image_file"]["name"])) {
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_file')) {
echo $this->upload->display_errors();
} else {
$course = $this->upload->data();
print_r($course);
$images1 = base_url() . 'upload/' . $course['file_name'];
$data = array(
'course_name' => $this->input->post('coursename'),
'author_name' => $this->input->post('authorname'),
'description' => $this->input->post('description'),
'price' => $this->input->post('price'),
'image_file' => $images1
);
print_r($data);
$this->load->model('Upload_model');
$id = $this->Upload_model->add_image($data);
if ($id > 0) {
$this->session->set_flashdata('feedback', 'Image added');
} else {
$this->session->set_flashdata('feedback', 'Image not added');
}
echo print_r($id);
echo '<img src="' . base_url() . 'upload/' . $course["file_name"] . '" width="300" height="225" class="img-thumbnail" />';
}
// $course = $this->upload->data();
// print_r($course);
// $images1 = base_url().'upload/'.$course['file_name'];
// $fn = $course['file_name'];
// $course = array(
// 'course_name' => $name,
// 'image' => $fn
// );
// $this->Course_model->add_course($course);
}
}
}
The Upload_model
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//put your code here
class Upload_model extends CI_Model
{
public function _construct()
{
parent::_construct();
$this->load->database();
}
public function add_image($data)
{
$selected_row = $this->db->insert('course', $data);
return $selected_row;
}
}
The View
<html>
<head>
<title>INFS3202 Demo</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/bootstrap.css">
<script src="<?php echo base_url(); ?>assets/js/jquery-3.6.0.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<br /><br /><br />
<?php
if ($this->session->flashdata('feedback')) {
echo '
<div class="alert alert-sucess">
' . $this->session->flashdata("feedback") . '
</div>';
}
?>
<h3 align="center"> Upload your course</h3>
<form method="post" action="Main/ajax_upload" id="upload_form" align="center" enctype="multipart/form-data">
<?php echo form_open(base_url() . 'Main/ajax_upload'); ?>
<div class="form-group">
<label class="form-label">Course Name</label>
<input type="text" class="form-control" placeholder="Course name" required="required" name="coursename" value="<?php echo set_value('coursename'); ?>">
<span class="text-danger"><?php echo form_error('coursename'); ?></span><!-- //this will display a validation error for user_name field -->
</div>
<div class="form-group">
<label class="form-label">Author Name</label>
<input type="text" class="form-control" placeholder="Author Name" required="required" name="authorname" value="<?php echo set_value('authorname'); ?>">
<span class="text-danger"><?php echo form_error('authorname'); ?></span><!-- //this will display a validation error for user_name field -->
</div>
<div class="form-group">
<label class="form-label">Price </label>
<input type="number" class="form-control" placeholder="Price" required="required" name="price" value="<?php echo set_value('price'); ?>">
<span class="text-danger"><?php echo form_error('price'); ?></span>
</div>
<div class="form-grou w-75 mt-5 d-flex flex-start w-100">
<!-- <input type="hidden" name="id"> -->
<textarea class="form-control ml-3" id="description" placeholder="Enter Description" name="description" rows="6"></textarea>
</div>
<input type="file" name="image_file" id="image_file" />
<br />
<br />
<input type="submit" name="upload" id="upload" value="Upload" class="btn btn-info" />
<?php echo form_close(); ?>
</form>
<br />
<br />
<div id="uploaded_image">
</div>
</div>
</body>
<script>
$(document).ready(function() {
$('#upload_form').on('submit', function(e) {
e.preventDefault();
if ($('#image_file').val() == '') {
alert("Please Select the File");
} else {
$.ajax({
url: "<?php echo base_url(); ?>Main/ajax_upload",
//base_url() = http://localhost/tutorial/codeigniter
method: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data) {
$('#uploaded_image').html(data);
}
});
}
});
});
</script>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|