I have an error in my program it can't edit file. Where I var_dump($this->data[data]) data it found but not save in database
This is my controller
function edit_kegiatan($id='')
{
//set validation properties
$this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
$this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
$id = $this->uri->segment(4);
//run validation
// jika dia ingin update data atau form validation error
if ($this->form_validation->run() == true) {
$this->load->model('mkegiatan');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$datafoto=$this->upload->data();
$nm_file =$datafoto['file_name'];
$data = array(
'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
'nama_kegiatan' => $this->input->post('nama_kegiatan'),
'content' => $this->input->post('content'),
'image' => $nm_file
);
$this->mkegiatan->update_kegiatan($id,$data);
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
redirect(site_url('admin/kegiatan'));
}
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['data'] = $this->mkegiatan->get_by_id($id);
$this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
Any help appreciated.
Related
I am having a problem with inserting file_path to database and file upload in folder using Codeigniter, I have a form field where user can upload any jpg|png|gif file. But how will I get the file_path of that particular file uploaded and insert it into database and file are stored in floder.
my controller code
public function enqry(){
$data = array();
$postData = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10240;
$this->load->library('upload', $config);
//if add request is submitted
if ( ! $this->upload->do_upload('upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('imageUploadForm', $error);
}else {
print_r('Image Uploaded Successfully.');
}
if($this->input->post('postSubmit')){
//form field validation rules
$this->form_validation->set_rules('name', 'post name', 'required');
$this->form_validation->set_rules('email', 'post email', 'required');
$this->form_validation->set_rules('mobile', 'post number', 'required');
$this->form_validation->set_rules('nationality', 'post nationality', 'required');
$this->form_validation->set_rules('location','post location','required');
//prepare post data
$postData = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'nationality' => $this->input->post('nationality'),
'location'=>$this->input->post('location'),
'statuse' => '0',
'created_at' => date("Y-m-d H:i:s")
/*'passport_details'=>$this->input->post('passport_details')*/
);
//validate submitted form data
if($this->form_validation->run() == true){
//insert post data
$insert2 = $this->user_mod->insert_enq($postData);
if($insert2){
$this->session->set_userdata('success_msg', 'Post has been added successfully.');
redirect('/User_con/log/');
}else{
$data['error_msg'] = 'Some problems occurred, please try again.';
}
}
}
$data['post'] = $postData;
$data['title'] = 'Create Post';
}
//load the add page view
public function log_enq(){
$this->load->view('templates/header');
$this->load->view('enquiry_reg');
$this->load->view('templates/footer');
}
My view code
<div class="form-group">
<label for="title">File Upload</label>
<input type="file" name="upload" placeholder="upload">
</div>
My model code
public function insert_enq($data = array()) {
$insert2 = $this->db->insert('tbl_enquiry', $data);
if($insert2){
return $this->db->insert_id();
}else{
return false;
}
}
if (!$this->upload->do_upload('upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('imageUploadForm', $error);
}
else {
$data = $this->upload->data('file_name');
$image = 'your_path'.$data;
}
and add $image variable to your your query like
$arrayName = array(
'imapge' => $image,
'more_column' => $more_column_values);
public function enqry(){
$data = array();
$postData = array();
$postData['upload_path'] = './uploads/';
$postData['allowed_types'] = 'gif|jpg|png';
$postData['max_size'] = 10240;
$this->load->library('upload', $postData);
if (!$this->upload->do_upload('upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('user_con/error', $error);
}
else {
$data = $this->upload->data('file_name');
$image = 'your_path'.$data;
}
if($this->input->post('postSubmit')){
$this->form_validation->set_rules('name', 'post name', 'required');
$this->form_validation->set_rules('email', 'post email', 'required');
$this->form_validation->set_rules('mobile', 'post number', 'required');
$this->form_validation->set_rules('nationality', 'post nationality', 'required');
$this->form_validation->set_rules('location','post location','required');
$postData = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'nationality' => $this->input->post('nationality'),
'location'=>$this->input->post('location'),
'statuse' => '0',
'created_at' => date("Y-m-d H:i:s"),
'upload' => $image
);
if($this->form_validation->run() == true){
$insert2 = $this->user_mod->insert_enq($postData);
if($insert2){
$this->session->set_userdata('success_msg', 'Post has been added successfully.');
redirect('/User_con/log/');
}else{
$data['error_msg'] = 'Some problems occurred, please try again.';
}
}
}
$data['post'] = $postData;
$data['title'] = 'Create Post';
}
$config = array(
'upload_path' =>'./uploads/',
'allowed_types' =>'jpg|jpeg|png|gif',
'max_size' => '5000KB');
$this->load->library('upload',$config);
$this->upload->initialize($config);
Replace this code your code....and cheack......
im getting this error whenever i insert something into the database it insters the data but i recieve this error
Message: Cannot modify header information - headers already sent by (output started at project/application/controllers/add_products.php:79)
function do_upload() {
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$config['new_image'] = './assets/';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->form_validation->set_rules('name', 'Product Name', 'required|xss_clean');
$this->form_validation->set_rules('description', 'Product Description', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required');
if (!$this->upload->do_upload() || !$this->form_validation->run()) {
$error = array('error' => $this->upload->display_errors());
redirect('add_products');
} else {
$data = $this->upload->data();
$this->thumb($data);
$category = $_POST["prod_category"];
if($category == "2")
{
$category = $_POST["other_category"];
}
$file = array(
'img_name' => $data['raw_name'],
'thumb_name' => $data['raw_name'] . '_thumb',
'ext' => $data['file_ext'],
'product_name' => $this->input->post('name'),
'product_description' => $this->input->post('description'),
'product_price' => $this->input->post('price'),
'product_category' =>$category,
);
// $this->db->insert("product_category",array("category"=>$category));
$query = 'INSERT INTO product_category (id , category) VALUES (? , ?)
ON DUPLICATE KEY UPDATE category=VALUES(category)';
$this->User->insert_prod($file);
$data = array('upload_data' => $this->upload->data());
echo '<script>alert("You Have Successfully Added a new Product!");</script>';
redirect('admin_products','refresh');
$this->load->view('admin_products');
}
}
remove line
echo '<script>alert("You Have Successfully Added a new Product!");</script>';
put ob_start(); after first < ?php line
so your code should be:
<?php
ob_start();
it will turn on the output buffering
i'am not able to do upload image with lines of code in function add()
just provide me with some solution for uploading image
public function add()
{
if ($this->input->server('REQUEST_METHOD') === 'POST')
{
$this->form_validation->set_rules('image', 'image', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
$this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
if ($this->form_validation->run())
{
**$data_to_insert = array(
'image' => $this->input->post('image'),
'text' => $this->input->post('text'),**
);
if($this->home_banner_model->insert_home_banner($data_to_insert)){
$data['flash_message'] = TRUE;
}else{
$data['flash_message'] = FALSE;
}
}
}
$data['main_content'] = 'admin/home_banner/add';
$this->load->view('includes_admin/template', $data);
}
$config = array(
'upload_path' => 'uploadPath',
'allowed_types' => 'jpg,jpeg',
'max_size' => 1500
);
$this->load->library('upload', $config);
if(isset($_FILES['field_name']) && $_FILES[field_name]['name'] != NULL)
if($this->upload->do_upload('field_name'))
{
$upload_data = $this->upload->data();
$data_to_insert = array(
'attachment_link' => 'uploadPath/'.$upload_data['file_name'],
'attachment_size' => $upload_data['file_size']
// u can add parameters as for ur table to save
);
$this->home_banner_model->insert_home_banner($data);
}
else
{
echo $this->upload->display_errors();
}
I have a form which asks for user's name and description and an optional field of image uploading. When the user uploads the image I want the name of the image to be changed and stored into the database and if no image is uploaded then a default name is stored in the database. It works fine if user uploads an image but if he does not then nothing is being uploaded into the database.
This is the controller function :
function store()
{
$this->load->model('campus_m');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = preg_replace('/[^a-z0-9]+/i','-',iconv('UTF-8','ASCII//TRANSLIT',$this->input->post('name')));
$config['file_name'] = trim($config['file_name'],'-').now().'.jpg';
$this->load->library('upload', $config);
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('goods', 'Goods', 'required');
$this->form_validation->set_rules('name', 'Name', 'required|max_length[12]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('campus_write_v');
}
else
{
print_r($_FILES['userfile']);
if (empty($_FILES['userfile'])) {
if(!$query = $this->campus_m->create_review("Marla-overdoses1360186300.jpg")){
$data['write_campus'] = 'The answer has not been stored.';
$this->load->view('campus_write_v', $data);
}
else {
$data['write_campus'] = 'The answer has been stored. ';
$this->load->view('campus_write_v', $data);
}
}
else
{
if($this->upload->do_upload()){
if(!$query = $this->campus_m->create_review($config['file_name'])){
$data['write_campus'] = 'The answer has not been stored.';
$this->load->view('campus_write_v', $data);
}
else {
$data['write_campus'] = 'The answer has been stored. ';
$this->load->view('campus_write_v', $data);
}
}
else
{
$error = array('error' => $this->upload->display_errors());
foreach ($error as $rows => $r)
{
echo $r ;
}
$this->load->view('campus_write_v');
}
}
}
}
And this is the view :
<?php
$attributes = array('id' => 'contactform');
echo form_open_multipart('campus/store', $attributes);
?>
<div>
<?php
echo form_label('Title of Area');
$data = array(
'name' => 'name',
'id' => 'name',
'placeholder' => 'Location and name of the place',
'required' => 'required',
'value' => set_value("name")
);
echo form_input($data);
?>
</div>
<div>
<?php
$data = array(
'name' => 'goods',
'id' => 'goods',
'placeholder' => 'Tell us about the place',
'required' => 'required',
'value' => set_value("goods"),
'rows' => '20',
'cols' => '50'
);
echo form_textarea($data);
?>
</div>
<div>
<input type="file" id="userfile" name="userfile" size="20" />
</div>
<div>
<?php
$data = array(
'class' => 'button',
'value' => 'Submit',
'id' =>"submit"
);
echo form_submit($data);
?>
</div>
<?php echo form_close();
?>
</div>
I will solve this in this way:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$data = $this->upload->data();
$photo['image'] = $data['file_name']; // Name of image
} else {
$photo['image'] = "Name"; // Name that you want
}
This is the short code of uploads controller.
Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,
this is my model file:
function uploadImg()
{
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000,
'encrypt_name' => true
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 200,
'height' => 200,
'encrypt_name' => true,
'max_size' => 2000
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
# Ret profil billed navn #
$file_array = $this->upload->data('file_name');
return $billed_sti['billed_sti'] = $file_array['file_name'];
//$this->db->where('username', $this->input->post('username'));
//$this->db->update('users', $profilBilledNavn);
}
This is my controller:
function opret() {
$this->form_validation->set_rules('overskrift', 'overskrift', 'required');
$this->form_validation->set_rules('description', 'description', 'required');
$this->form_validation->set_rules('indhold', 'indhold', 'required');
if($this->form_validation->run() == true)
{
$this->load->model('admin/nyheder_model');
$billed_sti = $this->nyheder_model->uploadImg();
$data = array(
'overskrift' => $this->input->post('overskrift'),
'description' => $this->input->post('description'),
'indhold' => $this->input->post('indhold'),
'billed_sti' => $billed_sti,
'brugernavn' => $this->session->userdata('username'),
'godkendt' => 'ja'
);
$this->db->insert('nyheder', $data);
redirect('admin/nyheder/index');
} else {
$this->index();
}
}
I do the image processing in the controller rather than the model.
"Models are PHP classes that are designed to work with information in your database."
from: http://codeigniter.com/user_guide/general/models.html
What you need to do is move the code for uploading the image to the controler.
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
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);
}
}
Once you did that,
You can insert the name of the file from the $data variable created in this line:
$data = array('upload_data' => $this->upload->data());
and you can get the value like this:
$data['file_name']
The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.
I hope it helps.
Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below
//Controller Class
function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit
$this->upload->initalize($config);
if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement
Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.
Thanks
J