I'm trying create a website using codeigniter. The structure that I use is only using view and controller. The model is directly in controller.
So, i got this error message:
Severity: Notice
Message: Undefined variable: view
Filename: template/default.php
Line Number: 146
And this is my code on controller (artikeladmin.php)
public function add()
{
if($this->input->post())
{
$judul = $this->input->post('judul');
$kategori_id = $this->input->post('kategori_id');
$tanggal = $this->input->post('tanggal');
$gambar = $this->input->post('gambar');
$published = $this->input->post('published');
$deskripsi = $this->input->post('deskripsi');
$config['upload_path'] ='assets/images/upload/';
$config['allowed_types'] ='jpeg|png|gif|jpg';
$config['max_size'] ='*';
$config['max_width'] ='*';
$config['max_height'] ='*';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$data['error'] = array('error' =>$this->upload->display_errors());
$this->load->view('admin/template/default', $data);
}
else
{
$attr = $this->upload->data();
$args = array(
'gambar' => $attr['file_name'],
'kategori_id' => $kategori_id,
'judul' => $judul,
'published' => $published,
'tanggal' => $tanggal,
'deskripsi' => $deskripsi
);
$this->db->insert('artikel', $args);
$this->session->set_flashdata('message','Data berhasil disimpan');
redirect('artikeladmin');
}
}
$user = $this->session->userdata('myuser');
if(!isset($user) or empty($user))
redirect('login');
$sql = " SELECT * FROM kategori";
$query = $this->db->query($sql);
$kategori = $query->result_array();
$data['view'] = 'admin/add_article';
$data['kategori'] = $kategori;
$data['action'] = 'artikeladmin/add/';
$this->load->view('admin/template/default', $data);
}
And this is my view (template/default.php)
<div id="content" class="span10">
<?php $this->load->view($view); ?>
</div>
in my controller you can see that i have define variable view like this
$data['view'] = 'admin/add_article';
And im trying to add new variable like
$data['view'] = $data;
but the error still happen
If do_upload() fails it will load the view without the $view variable being set:
if(!$this->upload->do_upload())
{
$data['error'] = array('error' =>$this->upload->display_errors());
$this->load->view('admin/template/default', $data);
}
default.php looks for $view
<?php $this->load->view($view); ?>
and you get the Undefined variable error.
Try instantiating $view before any calls to default.php are made.
Related
I made a codeigniter function so when a user selects a product picture to upload, it must show as preview picture in the form which the user selected. That way a user can see which picture he selected.
This is a link of how it should look like : https://imgur.com/a/2b775
index function in controller:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
This is the upload function in the controller code:
public function upload() {
$this->load->library('upload');
$this->load->library('image_lib');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name']= true;
$this->upload->initialize($config);
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
if(!$this->upload->do_upload('userfile')){
$error = array('error'=>$this->upload->display_errors());
$this->load->view('product_form', $error);
}else{
//Main image
$data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
$config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 547;
$config['height'] = 430;
//Thumb image
$dataThumb = $this->upload->data();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = FALSE;
$configThumb['width'] = 120;
$configThumb['height'] = 112;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this-> db-> insert('products', array(
'user_id' => $_SESSION['user_id'],
'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
'product_naam' => $this->input->post('product_naam'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
'date_created' => date('Y-m-d'),
'date_updated' => date('Y-m-d')
));
$data['img'] = base_url().
'/upload/new_'.$data["raw_name"].$data['file_ext'];
$dataThumb['img'] = base_url().
'/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
header('location:https://kadokado-ferran10.c9users.io//Product/cadeaupagina_aangeboden');
}
}
And this is the view form file of product_foto row:
<div class="image-upload">
<input id="file-input" type="file" name="userfile"/>
<h3>Upload foto </h3>
<label for="file-input">
<img src="../images/PLUS.jpg"/>
</label>
</div>
</td>
<?php if ($get_image) {
echo '<tr>';
echo '<td>'.img(array('width' => '120', 'height' => '80', 'src' => 'upload/'.$data->product_foto)).'</td>';
echo '<td> Bekijk | Verwijder </td>';
echo '</tr>';
}
else {
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
} ?>
</tr>
The 2 erros that I'm getting:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: controllers/Product.php
Line Number: 17
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 17
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
2nd error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get_image
Filename: views/product_form.php
Line Number: 67
Backtrace:
File: /home/ubuntu/workspace/application/views/product_form.php
Line: 67
Function: _error_handler
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 21
Function: view
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
Database information and other information:
The map where the picture is stored: upload
The database table name: products
The product picture row is named: product_foto
Excluding any possible issues with your upload code, the reason you are getting these errors is because you are assigning $data to the class scope $this->data and not accessing $this->data but just $data in your view call.
Revised code:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? (!empty($products->result()) ? $products->result() : false ) : false;
$data = array('get_image' => $products);
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
As a side note I don't think you have to access $this->upload->data(); twice as its the same data. Just assign it to an analogous variable like $image.
i'm new to codeigniter. i need help to upload a picture and video and save it to folder and database.
here is my controller
public function upload()
{
$this->m_upload->upload();
$this->upload_gambar();
}
public function upload_gambar()
{
//load the helper
$this->load->helper('form');
$config['upload_path'] = 'assets/gallery/images';
$config['allowed_types'] = 'gif|jpg|png|mp4';
$config['max_size'] = '';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
$data['upload_data'] = '';
$this->upload->do_upload('uploadan');
redirect(c_upload);
}
and this is my models
public function upload()
{
$title = $this->input->post('title');
$details = $this->input->post('details');
$type = $this->input->post('gallery');
$picture = $_FILES['uploadan']['name'];
$data = array(
'url' => $picture,
'title' => $title,
'details' => $details,
'category' => $type
);
$this->db->insert('gallery', $data);
}
i've already set upload_max_filesize and post_max_size in the php.ini but it still not working. please help me fix this problem. thankyou
Try this
In Controller
$configVideo['upload_path'] = 'assets/gallery/images'; # check path is correct
$configVideo['max_size'] = '102400';
$configVideo['allowed_types'] = 'mp4'; # add video extenstion on here
$configVideo['overwrite'] = FALSE;
$configVideo['remove_spaces'] = TRUE;
$video_name = random_string('numeric', 5);
$configVideo['file_name'] = $video_name;
$this->load->library('upload', $configVideo);
$this->upload->initialize($configVideo);
if (!$this->upload->do_upload('uploadan')) # form input field attribute
{
# Upload Failed
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('controllerName/method');
}
else
{
# Upload Successfull
$url = 'assets/gallery/images'.$video_name;
$set1 = $this->Model_name->uploadData($url);
$this->session->set_flashdata('success', 'Video Has been Uploaded');
redirect('controllerName/method');
}
In Model
public function uploadData($url)
{
$title = $this->input->post('title');
$details = $this->input->post('details');
$type = $this->input->post('gallery');
$data = array(
'url' => $url,
'title' => $title,
'details' => $details,
'category' => $type
);
$this->db->insert('gallery', $data);
}
Add mimes code for media file in:
application/config/mimes.php
especially for mp4
When I submit my form on my main index I am trying to pass a variable call $store_id to a function on same controller called do_upload_image($store_id)
I am trying to be able to get the last id that was inserted in my store table.
It works fine when loading another model function $this->example_model->add($store_id); but not when doing it on another function on same controller.
Error Output
Column 'store_id' cannot be null
INSERT INTO `setting` (`store_id`, `group`, `key`, `value`) VALUES (NULL, 'config', 'config_image', 'flagnz.png')
Like so
$store_id = $this->model_store_add->add_store(); // this way should let me get id that was inserted.
$this->do_upload_image($store_id);
How am I able to get the $store_id to pass through another function on same controller? I have a model which trying to pass the store id to on do_upload_image
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Store_add extends MX_Controller {
public $store_id;
public function __construct() {
parent::__construct();
if (!$this->session->userdata('isLogged')) {
redirect('admin');
}
$this->load->model('admin/setting/model_store_add');
$this->load->model('admin/setting/model_store_get');
$this->lang->load('admin/setting/store_add', $this->settings->get('config_admin_language'));
}
public function index()
$this->load->library('form_validation');
$this->form_validation->set_rules('config_image', 'Store Image','callback_do_upload_image');
if ($this->form_validation->run($this) == FALSE) {
return $this->load->view('setting/store_add.tpl');
} else {
// $store id lets me get id that was inserted from store table
$store_id = $this->model_store_add->add_store();
$this->model_store_add->add_config_meta_title($store_id); // Works
$this->do_upload_image($store_id);
$this->session->set_flashdata('success', $this->lang->line('text_success'));
redirect('admin/setting/store');
}
}
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (is_uploaded_file($_FILES['config_image']['tmp_name'])) {
if (!$this->upload->do_upload('config_image')) {
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
$resize_data = $this->upload->data();
$config_resize['image_library'] = 'gd2';
$config_resize['source_image'] = './image/upload/' . $resize_data['file_name'];
$config_resize['new_image'] = './image/cache/' . $resize_data['file_name'];
$config_resize['maintain_ratio'] = false;
$config_resize['create_thumb'] = false;
$config_resize['width'] = 100;
$config_resize['height'] = 100;
$this->load->library('image_lib', $config_resize);
$this->image_lib->initialize($config_resize);
if (!$this->image_lib->resize()) {
$this->sessions->set_flashdata('error', $this->image_lib->display_errors());
redirect('admin/setting/setting');
} else {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
}
}
}
Model on do_upload_image
public function add_config_image($store_id = 0, $file_data = array()) {
$file_data = $this->upload->data();
$data = array(
'store_id' => $store_id,
'group' => "config",
'key' => "config_image",
'value' => $file_data['file_name']
);
$this->db->set($data);
$this->db->insert($this->db->dbprefix. 'setting');
}
Model add
public function add_store() {
$data = array(
'name' => $this->input->post('config_name'),
'url' => $this->input->post('config_url'),
'ssl' => $this->input->post('config_ssl')
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'store');
return $this->db->insert_id();
}
I have narrowed down my own problem. I need to pass the store id a second time as a if statement and now all works
I just removed the resize function for testing to see if that was issue but now no why. Also need to add return true below for callback function.
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('config_image')) {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
return true;
} else {
return false;
}
}
I'm curious to know in $this->upload->do_upload('img') field name is passing mandotory not.I have seen several example in stackoverflow where do_upload() not taking any argument as file field name.But in case of mine without field name file not uploaded.I want to know which is correct syntax?
2)How do i bypass the file upload validation when there is no file being uploaded.If there is no file(image) in the form then $this->upload->display_errors() will not be called.my code is below
function add()
{
if ($this->input->server('REQUEST_METHOD') === 'POST')
{
$this->form_validation->set_rules('category', 'Category Name', 'required');
if ($this->form_validation->run())
{
$data_to_store = array(
'category' => $this->input->post('category'),
'description' => $this->input->post('description'),
'parent'=>'0'
);
$last_id=$this->admin_category_model->add_category($data_to_store);
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png|GIF|JPG|PNG';
$config['remove_spaces'] = TRUE;
$config['max_size'] = '0';
$config['file_name'] =$last_id.'_'.$_FILES['img']['name'];
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if($this->upload->do_upload('img'))
{
$data = array('upload_data' => $this->upload->data());
$config2['image_library'] = 'gd2';
$config2['source_image'] = $data['upload_data']['full_path'];
$config2['new_image'] ='./uploads/thumb/'.$data['upload_data']['file_name'];
$config2['create_thumb'] = FALSE;
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 35;
$config2['height'] = 35;
$this->load->library('image_lib',$config2);
$this->image_lib->resize();
$data_to_store = array(
'img' => $config['file_name'],
);
$this->admin_category_model->update_category($last_id,$data_to_store);
$this->session->set_flashdata('flash_message', 'Record Added');
redirect('admin_category/index');
}
else
{
$data['error']=$this->upload->display_errors();
}
}
}
$data['title']='Add Category';
$data['main_content'] = 'admin/add_category';
$this->load->view('admin/includes/template', $data);
}
1st) No, not necessary, by default it will take the name userfile.
2nd) Say for example your fieldname is img check like this:
if( $_FILES['img']['name'] != "" ){
//your upload code here
}
I am trying to view my image files taken from the database and i have no idea why i get a undefined variable when loading my view.
Pretty much I am just trying to view the imagenames stored in my database images.
Any help would be great thank you.
Here are the errors i receive when viewing in browser
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: imagename
Filename: layouts/home.php
Line Number: 8
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: layouts/home.php
Line Number: 8
Here is my Controller
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('upload');
}
function index() {
$this->load->model('index_model');
$foldername['foldername'] = $this->index_model->getFoldernames();
$imagename['imagename'] = $this->index_model->getImagenames();
$this->load->view('layouts/home', $foldername, $imagename, array('error' => ' ' ));
}
function create() {
if(array_key_exists('createFolder',$_POST)){
$data = array(
'folderName' => $this->input->post('folderName')
);
$data = str_replace(' ', '_', $data);
$this->index_model->createFolder($data);
}
$this->foldercreated();
}
function delete() {
$this->load->model('index_model');
$foldername['foldername'] = $this->index_model->getFoldernames();
$this->load->view('layouts/delete', $foldername);
}
function deleteFolder() {
if($this->input->post('checkbox') !== false) {
$checkbox = $this->input->post('checkbox');
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
function foldercreated() {
$this->load->view('partials/foldercreated');
}
function folderdeleted() {
$this->load->view('partials/folderdeleted');
}
function do_upload() {
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile'))
{
// echo '<p>IMAGE NOT WORKING</p>'. '<br/><p>' . $this->upload->display_errors() . '</p>';
$uploadError = $this->upload->display_errors();
$this->load->view('partials/upload_error', $uploadError);
}
else
{
// $imagefile = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$db_data = array('imageName' => $upload_data['file_name']);
$this->index_model->addImage($db_data); // replace the tablename
$this->load->view('partials/upload_success', $db_data);
}
}
}
Here is my Model
function getFolderNames() {
$this->db->order_by('id', 'DESC');
$this->db->select('folderName');
$q = $this->db->get('senior');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
function getImagenames() {
$this->db->order_by('id', 'DESC');
$this->db->select('imageName');
$query = $this->db->get('images');
if($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
And Here is my View
<div id="imagefiles">
<?php
foreach ($imagename as $img) { ?>
<div><?php echo $img->imageName; ?></div>
<?php } ?>
</div>
You aren't passing data to your view correctly.
You have this: $this->load->view('layouts/home', $foldername, $imagename, array('error' => ' ' ));
Your second parameter should be an array of data that you want to pass to your view. Try something like this:
$data['foldername'] = $this->index_model->getFoldernames();
$data['imagename'] = $this->index_model->getImagenames();
$this->load->view('layouts/home', $data);
Please check the CodeIgniter documentation for more info.