My All Data has been updated successfully but Image not update and old image not remove. I am using Query Builder.
Can anyone tell me what to do?
My Controller are given below
I want to know something new.
public function update(Request $request,$id)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'title' => 'required',
'details' => 'required',
]);
$data = array();
$data['name'] = $request->name;
$data['title'] = $request->title;
$data['details'] = $request->details;
$data['status'] = $request->status;
$image=$request->photo;
if ($image) {
$image_name = str_random(20);
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/upload/event';
$image_url = $upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
if ($success) {
$data['photo'] = $image_url;
$img = DB::table('events')->where('id',$id)->first();
$image_path = $img->photo;
$done = unlink($image_path);
$user = DB::table('events')->where('id',$id)->update($data);
if ($user) {
$notification = array(
'messege' => 'Data Updated Successfully',
'alert-type' => 'success'
);
return redirect('admin/event')->with($notification);
}else{
return redirect()->back();
}
}
}else{
return redirect()->back();
}
//return redirect('admin/event')->with($notification);
}
I have a code like this, the problem is when I input why the id_user value is "0".
even though the admin logged in is "1"
Is there anything missing in my script code?
Controller
public function tambah() {
// $this->session->set_userdata('upload_image_file_manager',true);
$kategori = $this->kategori_m->tampil();
$this->session->set_userdata('upload_image_file_manager',true);
// Validasi
$valid = $this->form_validation;
$valid->set_rules('judul_berita','Judul','required',
array( 'required' => 'Judul harus diisi'));
$valid->set_rules('isi_berita','Isi','required',
array( 'required' => 'Isi berita harus diisi'));
if($valid->run()) {
if(!empty($_FILES['gambar']['name'])) {
$config['upload_path'] = './resch/dev/admin/assets/media/';
$config['allowed_types'] = 'gif|jpg|png|svg|jpeg';
$config['max_size'] = '20480'; // 2 Mb
$this->load->library('upload', $config);
if(! $this->upload->do_upload('gambar')) {
// End validasi
$data = array( 'head_title' => 'Berita- Edutech Solution',
'title' => 'Tambah Berita',
'head_menu' => 'Berita',
'sub_title' => 'Total Berita',
'kategori' => $kategori,
'error' => $this->upload->display_errors(),
'isi' => 'superadmin/berita/tambah');
$this->load->view('superadmin/template/wrapper_admin', $data, FALSE);
// Masuk database
}else{
$upload_data = array('uploads' =>$this->upload->data());
// Image Editor
$config['image_library'] = 'gd2';
$config['source_image'] = './resch/dev/admin/assets/media/'.$upload_data['uploads']['file_name'];
$config['new_image'] = './resch/dev/admin/assets/media/thumbs';
$config['create_thumb'] = TRUE;
$config['quality'] = "100%";
$config['maintain_ratio'] = TRUE;
$config['width'] = 360; // Pixel
$config['height'] = 360; // Pixel
$config['x_axis'] = 0;
$config['y_axis'] = 0;
$config['thumb_marker'] = '';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$i = $this->input;
$slug = url_title($i->post('judul_berita'),'dash',TRUE);
$data = array( 'id_kategori' => $i->post('id_kategori'),
'id_user' => $this->session->userdata('user_name'),
'slug_berita' => $slug,
'judul_berita' => $i->post('judul_berita'),
'isi_berita' => $i->post('isi_berita'),
'jenis_berita' => $i->post('jenis_berita'),
'status_berita' => $i->post('status_berita'),
'gambar' => $upload_data['uploads']['file_name'],
// 'icon' => $i->post('icon'),
'keyword' => $i->post('keyword'),
'tanggal_publish'=> date('Y-m-d',strtotime($i->post('tanggal_publish'))).' '.$i->post('jam_publish'),
// 'tanggal_mulai' => $i->post('tanggal_mulai'),
// 'tanggal_selesai' => $i->post('tanggal_selesai'),
// 'urutan' => $i->post('urutan'),
'tanggal_post' => date('Y-m-d H:i:s'),
);
$this->berita_m->tambah($data);
$this->session->set_flashdata('sukses', 'Data telah ditambah');
redirect(base_url('superadmin/berita/jenis_berita/'.$i->post('jenis_berita')),'refresh');
}}else{
$i = $this->input;
$slug = url_title($i->post('judul_berita'),'dash',TRUE);
$data = array( 'id_kategori' => $i->post('id_kategori'),
'id_user' => $this->session->userdata('user_name'),
'slug_berita' => $slug,
'judul_berita' => $i->post('judul_berita'),
'isi_berita' => $i->post('isi_berita'),
'jenis_berita' => $i->post('jenis_berita'),
'status_berita' => $i->post('status_berita'),
'gambar' => $upload_data['uploads']['file_name'],
// 'icon' => $i->post('icon'),
'keyword' => $i->post('keyword'),
'tanggal_publish'=> date('Y-m-d',strtotime($i->post('tanggal_publish'))).' '.$i->post('jam_publish'),
// 'tanggal_mulai' => $i->post('tanggal_mulai'),
// 'tanggal_selesai' => $i->post('tanggal_selesai'),
// 'urutan' => $i->post('urutan'),
'tanggal_post' => date('Y-m-d H:i:s'),
);
$this->berita_m->tambah($data);
$this->session->set_flashdata('sukses', 'Data telah ditambah');
redirect(base_url('superadmin/berita/jenis_berita/'.$i->post('jenis_berita')),'refresh');
}}
// End masuk database
$data = array( 'head_title' => 'Berita- Edutech Solution',
'title' => 'Tambah Berita',
'head_menu' => 'Berita',
'sub_title' => 'Total Berita',
'kategori' => $kategori,
'isi' => 'superadmin/berita/tambah');
$this->load->view('superadmin/template/wrapper_admin', $data, FALSE);
}
in a model like this, I feel there is no problem
Model
public function tampil() {
$this->db->select('berita.*, user.nama, kategori.nama_kategori, kategori.slug_kategori');
$this->db->from('berita');
// Join dg 2 tabel
$this->db->join('kategori','kategori.id_kategori = berita.id_kategori','LEFT');
$this->db->join('user','user.id_user = berita.id_user','LEFT');
// End join
$this->db->order_by('id_berita','DESC');
$query = $this->db->get();
return $query->result();
}
// Tambah
public function tambah($data) {
$this->db->insert('berita',$data);
}
session userdata has null initials, not inputted to the database and
because I was confused, I skip. and create a function to update, but when the update function is executed it will give an error "too few arguments to functio"
I don't understand the problem.
Database output: userfile rather than image name?
rather than
CONTROLLER:
my upload directory is fine but when i use another method and call it when needed it doesnt give my desired output.. im very sorry for novicity
`
public function uploadImage()
{
$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
echo "<script>window.alert('failed to load UserFile');</script>";
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
echo "<script>window.alert('your image has been successfully uploaded');</script>";
}
}
`
public function signupPost()
{
if ($this->form_validation->run('signup') == FALSE) {
//fails wont continue to next page
$this->signup_view();
} else {
//upload image
$this->uploadImage();
//insert data into the database
$data = array(
'userId' => $this->input->post('userId'),
'userfile' => $this->input->post('userfile'),
);
//user data has successfully signed up
$this->User->signup_client($data);
redirect('http://localhost/GFC/index.php/main','refresh');
}
}
VIEW:
<?php
$attributes = array("name" => "signupform");
$hidden = array('userId' => 'userId', 'userfile' => 'userfile');
echo form_open_multipart("Client_Dashboard/signupPost", $attributes, $hidden);
?>
<div class="w3-row">
<input type="file" name="userfile" size="20" style="margin-left:30%; margin-top:8%;"/>
<span class="text-danger"><?php echo form_error('userfile'); ?></span>
</div>
<div class="container" style="padding:0%;">
<br>
<input type="submit" value="Register" class=" w3-btn w3-teal w3-large w3-hover-white w3-padding-large" />
</div>
<?php echo form_close(); ?>
You are not specifying the input and not returning the name of image to your function
function uploadImage() {
$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
// var_dump($error); die; // check error
} else {
$fileName = $this->upload->data();
return $fileName;
}
}
function signupPost() {
if ($this->form_validation->run('signup') == FALSE) {
// fails wont continue to next page
$this->signup_view();
} else {
// upload image
$data['UserFile'] = $this->uploadImage();
// insert data into the database
$data = array(
'userId' => $this->input->post('userId'),
'password' => $this->input->post('password'),
'emailAddress' => $this->input->post('emailAddress'),
'userfile' => $this->input->post('userfile'),
'branchId' => $this->input->post('branchId')
);
// user data has successfully signed up
$this->User->signup_client($data);
redirect('http://localhost/GFC/index.php/main', 'refresh');
}
}
I have Upload.php page as follows=>
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pgn';
$config['max_size'] = 0;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$config['detect_mime'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
and the Hesaplama.php controller as =>
$upload_data = $this->upload->data(); //(line 51) Should return array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$file = fopen("<?php echo site_url('uploads/$file_name'); ?>", "r");
while(! feof($file))
{...
However I get the following error =>
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Hesaplama::$upload
Filename: controllers/Hesaplama.php
Line Number: 51
Backtrace:
File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 51
Function: _error_handler
File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 249
Function: pgn_oku
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
How can we remedy that and get the file name of the newly uploaded file?
I personally think that the program does not recognize the data in another page(controller).
Thank you...
Sorry i can't comment yet, so i write it here. I think the library for
$this->upload->data();
wasn't loaded.
if you want to get only the filename, maybe you can try using session.
in Upload controller:
$data = $this->upload->data();
$this->session->set_userdata('filename', $data['file_name']);
in Hesaplama :
$filename = 'uploads/'.$this->session->userdata('filename');
$file = fopen("<?php echo site_url('$filename'); ?>", "r");
hope this help.
to get the filename and extention file to save it in a db you can use tis
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pgn';
$config['max_size'] = 0;
$config['detect_mime'] = TRUE;
$this->load->library('upload', $config);
if( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$upload_data = $this->upload->data();
/*
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
*/
$this->model->save_img_info($upload_data['file_name']);
$data = array('upload_data' => $upload_data );
$this->load->view('upload_success', $data);
}
}
}
?>
I'm a beginner in CodeIgniter. I'm trying to get the properties for one file that I've uploaded, like name, type, etc. But I can't retrieve that information. It's outputing an empty array. Where I'm doing wrong? I tried:
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = '999999';
$config['max_width'] = '3000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
$details = $this->upload->data();
echo "<pre>";
print_r ($details);
echo "</pre>";
if (!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
The output:
Array
(
[file_name] =>
[file_type] =>
[file_path] => ./uploads/
[full_path] => ./uploads/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
$this->upload->data() will return the uploaded data after executing the $this->upload->do_upload(). So try in your else part.
For more details, refer CI user Guide