Код: Выделить всё
defined('BASEPATH') OR exit('No direct script access allowed');
class Image extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Image_model');
}
public function index()
{
$this->load->view('image_upload');
}
// add image from form
public function add_image()
{
// CI form validation
$this->form_validation->set_rules('image_name', 'Image Name', 'required');
if ($this->form_validation->run() == FALSE){
$this->load->view('image_upload');
}
else {
// configurations from upload library
$config['upload_path'] = './assets/images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000'; // max size in KB
$config['max_width'] = '20000'; //max resolution width
$config['max_height'] = '20000'; //max resolution height
// load CI library called upload
$this->load->library('upload', $config);
// body of if clause will be executed when image uploading is failed
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
// This image is uploaded by deafult if the selected image in not uploaded
$image = 'no_image.png';
}
// body of else clause will be executed when image uploading is succeeded
else{
$data = array('upload_data' => $this->upload->data());
$image = $_FILES['userfile']['name']; //name must be userfile
}
$this->Image_model->insert_image($image);
$this->session->set_flashdata('success','Image stored');
redirect('Image');
}
}
// view images fetched from database
public function view_images()
{
$data['images'] = $this->Image_model->get_images();
$this->load->view('image_view', $data);
}
public function delete($image_id){
// Check whether id is not empty
if($image_id){
$galleryData = $this->image_data->getRows($image_id);
// Delete gallery data
$delete = $this->image_data->delete($image_id);
if($delete){
// Delete images data
$condition = array('image_id' => $image_id);
$deleteImg = $this->image_data->deleteImage($condition);
// Remove files from the server
if(!empty($galleryData['images'])){
foreach($galleryData['images'] as $img){
@unlink('uploads/images/'.$img['image']);
}
}
$this->session->set_userdata('success_msg', 'Gallery has been removed successfully.');
}else{
$this->session->set_userdata('error_msg', 'Some problems occurred, please try again.');
}
}
redirect($this->controller);
}
}
< /code>
Моя модель: < /p>
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Image_model extends CI_Model {
public function insert_image($image)
{
// assign the data to an array
$data = array(
'image_id' => $this->input->post('image_id'),
'image_name' => $this->input->post('image_name'),
'image' => $image
);
//insert image to the database
$this->db->insert('image_data', $data);
}
//get images from database
public function get_images()
{
$this->db->select('*');
$this->db->order_by('image_id');
$query = $this->db->get('image_data');
return $query->result();
}
public function delete($image_id){
// Delete gallery data
$delete = $this->db->delete($this->image_data, array('id' => $image_id));
// Return the status
return $delete?true:false;
}
}
Код: Выделить всё
Image upload
assets/js/bootstrap.min.js">
Image upload
assets/js/bootstrap.min.js">
< /code>
Я могу загрузить, но, пожалуйста, дайте мне знать процесс удаления из базы данных и папки. Когда я пытаюсь удалить, это показывает ошибку -< /p>
Было обнаружено необработанное исключение тип: сообщение об ошибке: невозможно использовать
объект типа stdclass в качестве массива < /p>
< /blockquote>
, пожалуйста, обновите код порции Delete, чтобы он удалялся как из Database и Plotser. />
База данных - ci_image
table - image_data column - image_id, image_name, image < /p>
< /blockquote>
Подробнее здесь: https://stackoverflow.com/questions/661 ... odeintiger
Мобильная версия