How to delete a image file? - codeigniter

How can i delete image files from uploaded folder in codeigniter from delete button??
Can any one guide me ???
Here is my controller that upload image files
private function upload() {
$config['upload_path'] = 'assets/uploads/orginal/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|x-png';
$config['max_size'] = '500';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$img = $this->upload->data();
// create thumbnail
$new_image = 'assets/uploads/thumbs/' . $img['file_name'];
$c_img_lib = array(
'image_library' => 'gd2',
'source_image' => $img['full_path'],
'maintain_ratio' => TRUE,
'width' => 100,
'height' => 100,
'new_image' => $new_image
);
$this->load->library('image_lib', $c_img_lib);
$this->image_lib->resize();
} else {
$this->data['error'] = $this->upload->display_errors();
}
}

Use the unlink() function:
unlink($new_image);
http://pt1.php.net/manual/en/function.unlink.php

Related

Undefine Variable id_gambar codeigniter

if I try to enter data, it can enter the database, but it doesn't display
// GAMBAR
public function gambar($id_program)
{
// panggil model
$program = $this->program_model->detail($id_program);
$gambar = $this->program_model->gambar($id_program);
// validasi input
$valid = $this->form_validation;
$valid->set_rules('judul_gambar', 'Judul/Nama Gambar', 'required',
array( 'required' => '%s harus diisi'));
if($valid->run()) {
$config['upload_path'] = './assets/upload/image/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400'; // KiloByte
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('gambar')) {
$data = array( 'title' => 'Tambah Gambar Program: '.$program->nama_program,
'program' => $program,
'gambar' => $gambar,
'error' => $this->upload->display_errors(),
'isi' => 'admin/program/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}else {
$upload_data = array('uploads' =>$this->upload->data());
// Image Editor
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload/image/'.$upload_data['uploads']['file_name'];
$config['new_image'] = './assets/upload/image/thumbs/';
$config['create_thumb'] = TRUE;
$config['quality'] = "100%";
$config['maintain_ratio'] = TRUE;
$config['width'] = 360; // Pixel
$config['height'] = 360; // Pixel
$config['thumb_marker'] = '';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// end create gambar
$inp = $this->input;
$data = array( 'id_gambar' => $id_gambar,
'judul_gambar' => $inp->post('judul_gambar'),
'gambar' => $upload_data['uploads']['file_name'],
);
$this->program_model->tambah_gambar($data);
$this->session->set_flashdata('sukses', 'Data gambar telah ditambah');
redirect(base_url('admin/program/gambar/'.$id_program),'refresh');
}}
$data = array( 'title' => 'Tambah Gambar Program: '.$program->nama_program,
'program' => $program,
'gambar' => $gambar,
'isi' => 'admin/program/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}
Is this working for you ?
'id_gambar' => $gambar["id_gambar"];
$id_gambar is not defined.

CodeIgniter Upload path is not valid

CodeIgniter upload class does is not returning the right full_path. I am trying to upload an image. I write up this code
$config['encrypt_name'] = TRUE;
echo $config['upload_path'] = FCPATH .'contest/videos/';
$config['max_size'] = '1000000000000000';
$config['allowed_types'] = '*';
$config['file_name'] = $_FILES['userfile']['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
But it keeps asking that the permission denied. When I check the $this->upload->data() I see the full_path does not have last trailing slash after the last folder. See the print_r result
Array
(
[file_name] => testvdo.mp4
[file_type] =>
[file_path] => /var/www/html/zebra_1975/contest/videos
[full_path] => /var/www/html/zebra_1975/contest/videostestvdo.mp4
[raw_name] => testvdo.mp4
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
There should be a slash after video in full_path. What I am doing wrong here?
Try this I hope it will help you to set yours properly.
if (isset($_FILES['profile_img']) && !empty($_FILES['profile_img']['name']))
{
$user_img_path = './public/uploads/images/users/';
$config['upload_path'] = $user_img_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['file_name'] = $img_name;
$config['max_size'] = '9999';
$config['max_width'] = '1524';
$config['max_height'] = '868';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('profile_img')){
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else{
$image = $this->upload->data();
print_r($image);
}
}
I am using this in my project
So for me I used this and is working for me
$user_img_path = './public/uploads/images/users/';
OR
$user_img_path = FCPATH'./public/uploads/images/users/';
OR
Editing Yours
$config['upload_path'] = FCPATH .'/contest/videos/';
Instead of
$user_img_path = FCPATH .'contest/videos/';
So try it. If it work for you let me know I am here to help

Codeigniter news image resize

How to resize the news picture? I never could.
My problem [source_image]
I use Codeigniter 3.1.6
Controller
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('news_image'))
{
$image = $this->upload->data();
$image_url = $image['file_name'];
$db_insert ='upload/news/'.$image_url.'';
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $db_insert,
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
echo "yes";
} else {
echo "no";
}
}
}
You can try this solution for your problem :
Controller :
<?php
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20240000245';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
// You can change width & height
$imgage_width= 60;
$imgage_height= 60;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
$error = $this->upload->display_errors();
// uploading failed. $error will holds the errors.
$this->form_validation->set_message('error',$error);
return FALSE;
} else {
$fdata = $this->upload->data();
$configer = array(
'image_library' => 'gd2',
'source_image' => $fdata['full_path'],
'maintain_ratio' => TRUE,
'width' => $imgage_width,
'height' => $imgage_height,
);
// Load Image Lib Library
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$img_data ['path'] = $config['upload_path'] . $fdata['file_name'];
// uploading successfull, now do your further actions
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $img_data ['path'],
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
$this->form_validation->set_message('success',"News has been added successfully.");
redirect('contorller/action_name');
} else {
$this->form_validation->set_message('error',"Error in aading news");
redirect('contorller/action_name');
}
}
}
?>
I Hope it will Help you.

How to update image data with form_upload in codeigniter

How to edit this data with image inside.Data have been updated when i press the button "submit", but the old image can not change with the new image
if($this->input->post('go_upload'))
{
validation.......
$IdPerumahan = $this->uri->segment(3);
if ($this->form_validation->run() == TRUE)
{
$lokasi_file = $_FILES['userfile']['tmp_name'];
$nama_file = $_FILES['userfile']['name'];
$ukuran_file = $_FILES['userfile']['size'];
$direktori_file = "./images/$nama_file";
if (empty($lokasi_file))
{
$this->load->model('PerumahanMDL');
$this->PerumahanMDL->SimpanUbahPerumahan($IdPerumahan);
redirect('PerumahanCON');
} else
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '2000';
$config['overwrite'] = 'true';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = "userfile";
if(!$this->upload->do_upload("userfile"))
{
$data['data']= 'TambahPerumahanVW';
$this->load->view('Desain2',$data);
}else
{
$asdf = $this->upload->data();
$config = array(
'source_image' => $asdf['full_path'],
'new_image' => './images/thumb',
'maintain_ration' => true,
'width' => 180,
'height' => 120
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$userfile = $_FILES['userfile']['name'];
$this->load->model('PerumahanMDL');
$this->PerumahanMDL->UbahDataPerumahan($userfile,$IdPerumahan);
redirect('PerumahanCON');
}
}
}
}
}
Add overwrite = true.
$this->load->library('upload', $config);
$this->upload->overwrite = true;

CI multiple image resizing

I'm uplaoding multiple images with CI, they do upload. I am also trying to resize them,
with the following code, only the first image resizes, rest of them don't. They get uploaded with their current size.
What's wrong?
Any help'd be really, really appreciated.
function doupload() {
$path = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value){
for($n=0; $n<=$count-1; $n++) {
$_FILES['userfile']['name']=$value['name'][$n];
$_FILES['userfile']['type'] = $value['type'][$n];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$n];
$_FILES['userfile']['error'] = $value['error'][$n];
$_FILES['userfile']['size'] = $value['size'][$n];
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$path[] = $data['full_path']; //contains full path of every image
}
}
foreach($path as $p=>$ath){
$config1 = array(
'source_image' => $ath,
'new_image' => './images',
'maintain_ration' => true,
'overwrite' => true,
'width' => 600,
'height' => 400
);
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
first load image_lib outside loop you can use initialize in loop and pass new config for every image
$this->load->library('image_lib');
foreach($path as $p=>$ath){
$config1 = array(
'source_image' => $ath,
'new_image' => './images',
'maintain_ration' => true,
'overwrite' => true,
'width' => 600,
'height' => 400
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
CodeIgniter's Loader class loads libraries only once, so in your foreach you resize the same image multiple times. Move the library loading out of the loop and use the initialize method of Image Manipulation library to set the config for every resize.
in my case load lib in constructor and use code
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = FALSE;
$config['source_image'] = $config['upload_path'].$image_info['file_name'];
$config['new_image'] = $config['upload_path']."thumb_".$image_info['file_name'];
$config['width'] = 313;
$config['height'] = 303;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$config['new_image'] = $config['upload_path']."icon_".$image_info['file_name'];
$config['width'] = 70;
$config['height'] = 70;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
keep index name always new_image in $config['new_image']

Resources