My image is uploading to images/news successfully. I want to now save that image in the database. but it keeps giving me the above error. My model query is correct because i use it to insert other data and it works.
How can i save the image into the database once the user clicks submit?
My controller:
function news()
{
$config = array(
'upload_path' => "./images/news",
'allowed_types' => "gif|jpg|png|jpeg|JPG",
'overwrite' =>False,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$this->load->helper(array('form', 'url'));
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('manage_news',$data);
}
else
{
$message2 = "Please choose another file type. gif,jpg,png,jpeg,pdf ";
echo "<script type='text/javascript'>alert('$message2'); </script>";
redirect('resetPasswordController/add_news', 'refresh');
}
$this->db->trans_start();
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
$this->load->model('users_model');
$this->users_model->insert($data);
$this->db->trans_complete();
$this->db->trans_complete();
redirect('resetPasswordController/manage_news', 'refresh');
}
my view:
<?php echo form_open_multipart('resetPasswordController/news');?>
<label>Image</label>
<input name = "userfile" type="file" />
my model:
function insert($data){
$this->db->insert('news',$data);
return $this->db->insert_id();
}
$data contain array values, you have to use upload_data
for ex :
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
change this to
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data['upload_data'] )
);
or
you can directly use upload data function
$data = array(
'image' => file_get_contents( $this->upload->data())
);
Related
In my Laravel-8, I have this code:
public function add(Request $request){
if( $request->ajax() ){
$rules = array(
'first_name.*' => 'required',
'country.*' => 'required'
);
$error = Validator::make($request->all(),$rules);
if($error->fails()){
return response()->json([
'error' => $error->errors()->all(),
]);
}
$first_name = $request->first_name;
$country = $request->country;
for( $count = 0; $count < count($first_name); $count++ ){
$data = array(
'first_name' => $first_name[$count],
'country' => $country[$count],
);
$insert_data[] = $data;
}
DynamicField::insert($insert_data);
return response()->json([
'success' => 'Data added Successfully',
]);
}
}
This successfully Inserts record.
How do I do the update to this same code?
I mean public function update ...
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.
I want to send email of invoice blade file as pdf but i getting following error
(1/1) Swift_IoException
Unable to open file for reading [/storage/D:\xampp\htdocs\clientreq\public/mypdf.pdf]
controller:
public function insert(Request $req)
{
ini_set('max_execution_time', 300000);
$mytime = Carbon::now();
$project_type=implode(',',$req->input('project_type'));
//$qty=implode(',',$req->input('qty'));
$amount=implode(',',$req->input('amount'));
$id=$req->input('id');
$bill_date=$req->input('bill_date');
$updated_at=$mytime->toDateTimeString();
$created_at=$mytime->toDateTimeString();
$data = array('client_id'=>$id,'date_of_bill'=>$bill_date,'description'=>$project_type,'amount'=>$amount,'created_at' => $created_at,'updated_at' => $updated_at);
DB::table('bill')->insert($data);
$client = DB::table('requirement')
->join('bill','requirement.id','=','bill.client_id')
->select('requirement.*','bill.*')
->where('requirement.id',$id)
->where('bill.date_of_bill',$bill_date)
->first();
$data = array(
'client_name' => $client->client_name,
'company_name' => $client->company_name,
);
$pdf = \PDF::loadView('pages.invoice',$data);
//return $pdf->stream();
$data = array(
'email_address'=>'seemashelar01#gmail.com',
);
Mail::send('pages.invoice', $data, function($message) use($data) {
$message->from('seemashelar01#gmail.com', 'PuraBox');
$message->to($data['email_address']);
$message->subject('hello');
$filename = \Storage::url(public_path() . '/' . 'mypdf.pdf');
$message->attach($filename);
//Full path with the pdf name
});
}
if you want address file from Storage you should call attachFromStorage , like this:
$message->attachFromStorage(
/path/to/file',
'name.pdf', [
'mime' => 'application/pdf'
]);
more info : https://laravel.com/docs/5.7/mail#attachments
Try this bellow example thats it
$data = array(
'name' => Input::get('name'),
'detail'=>Input::get('detail'),
'sender' => Input::get('sender')
);
$pdf = PDF::loadView('letters.test', $data);
Mail::send('emails.invoice', $data, function($message) use($pdf)
{
$message->from('us#example.com', 'Your Name');
$message->to('foo#example.com')->subject('Invoice');
$message->attachData($pdf->output(), "invoice.pdf");
});
I am using codeigniter transaction. i wrote a function but it is not working.It was supposed to complete the transaction while submitting my form. Now its not saving with this code.with out transition code it is working. how can i fix this:
public function twotable_insertData() {
$this->db->trans_start();
$data = array(
'brand_name' => $this->input->post('f_name'),
'brand_user_name' => $this->input->post('l_name'),
);
$brand_id = $this->m_common->insert_row('brands', $data);
echo '$brand_id';
$data1 = array(
'brand_id' => $brand_id,
'stadium_id' => $this->input->post('stadium'),
'concession_stand_no' => $this->input->post('con_std_no'),
);
$this->m_common->insert_row('concession_stands', $data1);
redirect('backend/brand/view_brand');
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
} else {
echo $this->db->trans_complete();
}
}
I have updated your query...
$this->db->trans_start();
$data = array(
'brand_name' => $this->input->post('f_name'),
'brand_user_name' => $this->input->post('l_name')
);
$brand_id = $this->m_common->insert_row('brands', $data);
// echo '$brand_id';
$data1 = array(
'brand_id' => $brand_id,
'stadium_id' => $this->input->post('stadium'),
'concession_stand_no' => $this->input->post('con_std_no')
);
$this->m_common->insert_row('concession_stands', $data1);
$this->db->trans_complete();
if($this->db->trans_status() === FALSE){
// Check if transaction result successful
$this->db->trans_rollback();
$this->session->set_flashdata('failure', 'Transaction Fails.');
}else{
$this->db->trans_complete();
$this->session->set_flashdata('success', 'Transaction Success.');
}
redirect('backend/brand/view_brand');
I have done the below code and I now get a database error -> Still cannot seem to get it to upload:
Error:
Column 'image_path' cannot be null
Database Structure
Controller:
class Addsale extends CI_Controller {
function __construct(){
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
// Main Page Data
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Add Sale';
$data['content'] = $this->load->view('admin/addsale', $data);
$this->load->view('admintemplate', $data);
//Set Validation
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural|required');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|required');
$this->form_validation->set_rules('condition', 'Condition', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
if($this->form_validation->run() === TRUE) {
$this->load->library('upload', $config);
$file_info = $this->upload->do_upload();
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE),
'image_path' => $file_info['full_path']
);
$this->sales_model->addSale($data);
}
}
function do_upload(){
//Set File Settings
$config['upload_path'] = './includes/uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
}
}
Model:
function addSale($data) {
$this->db->insert('sales', $data);
return;
}
Original Code:
Hello,
I have the following section of code:
if($this->form_validation->run() === TRUE) {
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE),
'image_path' => $this->input->post('userfile', TRUE)
);
$this->load->library('upload', $config);
$this->upload->do_upload();
}
}
What I am trying to do is when the form is valid save the data to the database (works fine) and upload the image.
Were I am struggling is that I cannot get the userfile "name" to save and the file will not upload.
Can this be done within the index function?
Files that are uploaded don't get included in post at all, so you can't get the name in the way you want it. You'll have to do something like this instead:
if($this->form_validation->run() === TRUE) {
$this->load->library('upload', $config);
$file_info = $this->upload->do_upload();
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE),
'image_path' => $file_info['full_path']
);
}
This will put the full path into your database if you just want the file name use 'client_name' or something (see Ross' answer for reference)
The function $this->upload->do_upload() returns an array of information about the file uploaded.
So:
$upload_data = $this->upload->do_upload();
print_r($upload_data); // to see everything
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"
)
So, depending on the value you need, you will want to do the upload first, and if it is successful, continue with processing the POST data. Then you can do this in your $data array:
'image_path' => $upload_data['file_name']; // adjust value to get what you want.
and you should be good to go.