Reputation: 41
actually, if Edit button is click and i do not change any fields and submit the form at the time creating again one more same name image and saving database also so how will resolve this issue .help me.
below the my code:
$foldername =$_POST['image_exists'];
//echo $foldername;die;
$old = umask();
clearstatcache();
if (! file_exists($foldername))
{
//echo "hdhdhdh";die;
$folderName = $_POST['event_name'].time();
$pathToUpload = 'images/events/' . $folderName;
//$create = mkdir($pathToUpload, 0777,true);
// chmod($pathToUpload,0755,true);
//if (! $create)
//return;
}
else{
//$folderName = $_POST['event_name'].time();
//$pathToUpload = 'images/events/' . $folderName;
echo $foldername."already exits";
}
umask($old);
$config['upload_path'] = $pathToUpload;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '9999';
$cofig['overwrite'] = 1;
$this->load->library('upload', $config);
if(isset($_POST['image_exists']) && $_POST['image_exists'] != '')
$temp_image = $_POST['image_exists'];
//print_r($temp_image);die;
if(isset($_FILES['event_image']['name']) && $_FILES['event_image']['name'] != "")
{
if ( ! $this->upload->do_upload('event_image'))
{
// $data = $this->upload->data();
echo "failed";
// echo
}
else
{
$data = $this->upload->data();
$eventimage = $data['file_name'];
//echo $eventimage;die;
}
}
else
{
$eventimage = $temp_image;
}
$event_name = ucwords(strtolower($this->input->post('event_name')));
$event_image = $folderName.'/'.$eventimage;
Upvotes: 1
Views: 80
Reputation: 1479
Alright Mr. Raja. I have Simulated your entire problem on my machine. May I suggest a little improvements in your code structure.
Step-1 . Database
Let's assume we have above ERD to implement with one table for events and other for event images. Let's put some sample data in it, create a view and render it in tabular format
Step-2. Image Upload
Clicked on the Images tab on First event. We got the form and you can see there is only one folder in uploads which is event. Lets upload some images
We tried to upload the images, two things happened success message and folder creation
Step-3. Delete Image
I have also placed a delete link here which not only deletes the image from the database but also delete from the directory. lets try that, I will delete one image
Here we go, the image is delete from the directory too.
Step-4. How it Works
I won't go into markup details of view, its standard Bootstrap, I am sure you can make them as per your designs. Lets talk about Controller first
List Events
public function events()
{
$data['events']=$this->Events_model->getEvents();
$this->load->view('events',$data);
}
That's it. Yes you must be wondering about getEvents Function in Event Model, lets see that.
public function getEvents()
{
return $this->db->get('events')->result_array();
}
Event Images Page
Controller function
public function event_images()
{
$eventId=$this->uri->segment(3);
$data['event']=$this->Events_model->getEventById($eventId);
$data['images']=$this->Events_model->getEventImages($eventId);
$this->load->view('event_images',$data);
}
Two Model function in it, getEventById which gets data for current event and getEventImages to show in case there are already event images in database.
Model Functions
public function getEventById($id)
{
$st=$this->db->SELECT('*')->from('events')->WHERE('id',$id)->get()->result_array();
return $st[0];
}
public function getEventImages($id)
{
$st=$this->db->SELECT('*')->from('event_images')->WHERE('event_id',$id)->get()->result_array();
if(!empty($st))
{
return $st;
}
else
{
return array();
}
}
Upload Image
The Form is calling do_upload function in the controller to upload image, I am using codeigniter upload library but before that I am making sure the path / directory exist. Following is the form opening line which will give you idea what is happening
<form action="<?php echo base_url().'Test/do_upload/'.$this->uri->segment(3)?>" method="post" enctype="multipart/form-data">
I am sending the event id to do_upload function in URL
Controller Function
public function do_upload()
{
$eventId=$this->uri->segment(3);
$path='uploads/events/'.$eventId;
if(!is_dir($path))
{
mkdir($path);
}
$config['upload_path'] = './'.$path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 500;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile1'))
{
$data['errors'] = $this->upload->display_errors();
$data['event']=$this->Events_model->getEventById($eventId);
$data['images']=$this->Events_model->getEventImages($eventId);
$this->load->view('event_images',$data);
}
else
{
$upload_data = $this->upload->data();
$file_name=$upload_data['file_name'];
$this->Events_model->updateEventImage($file_name,$eventId);
$data['success']='Congratulations! Image Updated Successfully';
$data['event']=$this->Events_model->getEventById($eventId);
$data['images']=$this->Events_model->getEventImages($eventId);
$this->load->view('event_images',$data);
}
}
Delete Image
Controller Function
public function DelImage()
{
$eventId=$this->uri->segment(3);
$imageId=$this->uri->segment(4);
$this->Events_model->delImage($imageId);
redirect(base_url().'Test/event_images/'.$eventId);
// OR redirect($_SERVER['HTTP_REFERER']);
}
Model Function
public function delImage($id)
{
$image=$this->db->select('*')->from('event_images')->WHERE('id',$id)->get()->row();
$path='uploads/events/'.$image->event_id.'/'.$image->image;
unlink($path);
$this->db->query('DELETE from event_images WHERE id='.$id);
}
Upvotes: 1