I can't access my controller array in my view in codeigniter. When I pass array in my view it give me unidentified variable error, please help me! This code is for change profile pic.
I want to display changed pic to my view! I can get photo path from my model to my view but can't access it to my view from my controller
My code is here
Model:
class Upload_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function update_photo($source) {
$username = $_SESSION['username'];
$pass = array('avatar'=> $source);
$this->db->update('users',$pass,array('username'=>$username));
$query = $this->db->get_where('users',array('avatar'=>$source,'username'=>$username),1 );
foreach ($query ->result() as $row)
{
return $row->avatar;
}
}
}
Controller:
class Upload_photo extends CI_Controller {
public $upload_model = "";
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
public function index() {
session_start();
if(isset($_FILES['file1']))
{
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
$source = base_url().'user/'.$u.'/Thumb/'.$filename;
$data["avatar"] = $this->upload_model->update_photo($source);
}
else
{
$this->load->view('profile_view',$data);
}
}
}
View - Could not access $avatar:
<div id="showimage" name='showimage'>
<?php
foreach ($avatar as $row)
{
echo $row;
}
?>
</div>
for in above controller you are defining $data["avatar"] in if clause true part, but you are load the view in else part. how it will take....
thats way it given as undifened variable..
once check your controller.
in controller
class Upload_photo extends CI_Controller{
public $upload_model = "";
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
public function index()
{
session_start();
if(isset($_FILES['file1']))
{
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
$source = base_url().'user/'.$u.'/Thumb/'.$filename;
$data['avatar'] = $this->upload_model->update_photo($source);
$this->load->view('profile_view',$data);
}
}
}
}
try this in view
<div id="showimage" name='showimage'>
<?php
isset($avatar){
foreach ($avatar as $row)
{
echo $row;
}
}
?>
Related
I am uploading multiple images in codeigniter framework. When i select multiple images, it upload only last selected image.
Below is my controller code, where i am doing wrong?
$files = $_FILES;
for($i=0; $i< count($_FILES['item_image']['name']); $i++)
{
$_FILES['item_image']['name']= $files['item_image']['name'][$i];
$_FILES['item_image']['type']= $files['item_image']['type'][$i];
$_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$i];
$_FILES['item_image']['error']= $files['item_image']['error'][$i];
$_FILES['item_image']['size']= $files['item_image']['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('item_image'))
{
$file_name=$this->upload->data('file_name');
$newdata = array(
'item_id'=>$last_id,
'image_name'=>$file_name
);
$this->Item_model->add_item_image($newdata);
$newdatatwo = array(
'item_image'=>$file_name
);
$this->Item_model->update_item($newdatatwo,$last_id);
$data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Item added Successfully!');
}else{
$data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Some Error, Please Try Again!');
}
}
The second condition in your for loop gets redefined in the first line of your for loop. I would check to make sure that isn't causing errors.
Refer The Below Code
Controller Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Multiple_upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
$this->load->view('upload');
}
public function Upload()
{
$files = $_FILES;
$config['upload_path'] = 'common_assets/images';
$config['allowed_types'] = 'jpg|gif|png';
$config['max_size'] = '';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '';
$config['max_height'] = '';
$config['max_filename'] = 0;
foreach ($_FILES['item_image']['name'] as $key => $value)
{
$_FILES['item_image']['name']= $files['item_image']['name'][$key];
$_FILES['item_image']['type']= $files['item_image']['type'][$key];
$_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$key];
$_FILES['item_image']['error']= $files['item_image']['error'][$key];
$_FILES['item_image']['size']= $files['item_image']['size'][$key];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!empty($value))
{
if (!$this->upload->do_upload('item_image'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit();
}else{
$file_name=$this->upload->data('file_name');
//**********Database Entry*********************
// $newdata = array(
// 'item_id'=>$last_id,
// 'image_name'=>$file_name
// );
// $this->Item_model->add_item_image($newdata);
// $newdatatwo = array(
// 'item_image'=>$file_name
// );
// $this->Item_model->update_item($newdatatwo,$last_id);
}
}
}
echo "<pre>Uploaded Successfully"; die;
}
}
?>
//This works for me:
$config['upload_path'] = 'common_assets/images';
$config['allowed_types'] = 'jpg|gif|png';
$config['max_size'] = '';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '';
$config['max_height'] = '';
$config['max_filename'] = 0;
$this->load->library('upload', $config);
foreach ($_FILES as $fieldname => $fileObject) {
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname)) {
$errors = $this->upload->display_errors();
redirect('error_page', $errors);
} else {
$data = array($fieldname => $this->upload->data());
extract($data[$fieldname]);
$$fieldname = $file_name;
}
}
}
$array_to_save = array('tbl_name_field_1' => $file_name_from_post_1,
'tbl_name_field_2' => $file_name_from_post_2);
I want to crop my image upload and change my profile photo, but I don't know why it's not working even the picture was upload, profile picture not change, you must logout and picture was be change. this is my controller.
private $pk = 'nim';
private $table = 'mahasiswa';
public function update()
{
$nim = $this->session->userdata('nim');
if($_POST) {
$file = $this->upload();
if ($this->m_data->update($this->pk, $nim, $this->table, $this->field_data($file['data']))) {
redirect('Mahasiswa/Profil');
}
} else {
redirect('Mahasiswa/Formubah');
}
}
private function field_data($file = '')
{
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
if ($file != '') {
$data['foto'] = $file['file_name'];
}
return $data;
}
public function upload()
{
$config['upload_path'] = './assets/image/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('foto')){
return[
'data' => $this->upload->display_errors(),
];
} else {
$data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['x_axis'] = 50;
$resize['y_axis'] = 50;
$resize['width'] = $img['config']['foto']['width'];
$this->image_lib->initialize($resize);
$this->image_lib->crop();
return [
'data' => $this->upload->data(),
];
}
}
i hope controller was enough. i forgot to tell, i use this show my picture. <?php echo base_url('assets/image/').$this->session->userdata('foto');?>
You are not properly implementing the library. Y forgot the library load and have not given its path.
$image_data = $this->upload->data();
$config['image_library'] = 'imagemagick';
$config['library_path'] = 'C:ImageMagick-7.0.1-3-portable-Q16-x86';//your library
$config['source_image'] = $image_data['full_path']; //get original image
$config['x_axis'] = 50;
$config['y_axis'] = 50;
$this->load->library('image_lib', $config);
if (!$this->image_lib->crop()) {
$this->handle_error($this->image_lib->display_errors());
}
I need to upload pictures and data create dynamically html and jQuery I use codeigneter framework and send the the all data to mysql.
This is code controller
public function Addtest() {
$this->load->library('upload');
$u= $this->session->userdata('id_user');
$files = $_FILES;
$cpt = count($_FILES['sliderimg']['name']);
$inputAll= $this->input->post();
$this->load->model('AddData');
for($i=0; $i < count($inputAll['artilce']); $i++) {
$_FILES['sliderimg']['name']= $files['sliderimg']['name'][$i];
$_FILES['sliderimg']['type']= $files['sliderimg']['type'][$i];
// $_FILES['sliderimg']['tmp_name']= $files['sliderimg']['tmp_name'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
var_dump($_FILES);
$data= array(
'id_user'=>$u,
'description2'=>$inputAll['description2'][$i],
'description1'=>$inputAll['description1'][$i],
'art'=>$inputAll['artilce'][$i],
$_FILES['sliderimg']['name']= $files['sliderimg']['name'][$i],
$image_name =>$_FILES['sliderimg']['name'][$i],
);
if( $this->AddData->Add_data($data)) {
echo 'false';
} else {
echo 'true';
}
and this the model code
public function Add_data($artt) {
extract($artt);
$data = array(
'Art'=>$art,
'des1'=>$description1,
'desc2'=>$description2,
'picture'=>$image_name,
'id_foreign'=>$id_user,
);
$this->db->insert('table',$data);
}
and the this the error output while I try to upload 2 pictures and data
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: image_name
and this is the set_upload_options() function
private function set_upload_options()
{
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['max_width'] = '20000';
$config['max_height'] = '17680';
$config['overwrite'] = FALSE;
return $config;
}
and the do_uplaod:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['max_width'] = '2024';
$config['max_height'] = '1068';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
return "false";
}
else
{
return "true";
}
}
I am building a user profile in codeigniter. I have set up a system that allows the user to create a folder with their user-id, and then upload photos to it. I have been abel to successfully upload files to a folder but I am just not sure how to display the photo in my view pertinent to the user.
I would assume I attach it to my $user[''] just not sure exactly how to do that. Thanks in advance.
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
public function img(array $user=array()) {
if(!isset($user['relation'])) {
return '';
}
$path = '';
if($user['relation'] != 'Choose One') {
$path = '/styles/images/Sharpie' . $user['relation'] . '.gif';
}
return $path;
}
}
Controller:
public function view_image($user_id)
{
$this->load->model('account_model');
$data['image'] = $this->account_model('model_that_will_fetch_relevant_image');
$this->load->view('where_image_is',$data);
}
model:
public function get_image($user_id)
{
$this->db->select('stuff'): \\ Select image filename
$this ->db->where('table_that_has_user_id', $user_id );
$query = $this->db->get($this->_table_name . ' as aliase'); \\ where filename is
return $query->result();
}
view:
<img src = "<?=site_url('path/on/server/' . $image['image_filename']); ?>">
This assumes you are storing the image filename in a mysql database.
class Users_model extends CI_Model {
var $table = 'tbl_admin';
function __construct(){
parent::__construct();
$this->load->database();
}
public function processLogin($username,$password){
$this->db->select('username,password');
$where = $array = array('username' =>$username,'password'=>$password);
$this->db->where($where);
$this->db->from($this->table);
$query = $this->db->get();
return $query;
}
}
Please use this code, I hope it will help you.
Using profile_image variable you can display image.
<?php
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$image = array('upload_data' => $this->upload->data());
$data['profile_image'] = $user_folder.$image['upload_data']['file_name'];
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
public function img(array $user=array()) {
if(!isset($user['relation'])) {
return '';
}
$path = '';
if($user['relation'] != 'Choose One') {
$path = '/styles/images/Sharpie' . $user['relation'] . '.gif';
}
return $path;
}
}
I am having trouble resizing photos that I am successfully uploading in my my upload module.
I am able to upload photos to the appropriate folder, however my resizing is not working and I also want to simultaneously create a thumbnail duplicate in the same folder.
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Try this:
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$this->load->library('image_lib'); #load the image manipulation library without initiatlising it here.
$configThumb['image_library'] = 'gd2';
//$configThumb['source_image'] = $user_folder;
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 50;
$configThumb['height'] = 50;
//$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$configThumb['source_image'] = $uploadedDetails['full_path'];
$this->image_lib->initialize($configThumb); #initialize the library here
$this->image_lib->resize(); #resize is done here
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Full path refers to the source image that is uploaded to your server. See the else condition you will get the idea. Now, the change is that we are loading the library and initializing it after uploading the image and hence we are getting the full path from $this->upload->data().
EDIT : MY WORKING FUNCTION TO CROP AN IMAGE IN TWO DIFFERENT SIZES
function udpate_profile($userId = 0){
$data = array();
//echo "<pre>";print_r($_POST);echo "</pre>";
/* Upload Image */
if($_FILES['image']['name'] != ""){
//echo "<pre>";print_r($_FILES);echo "</pre>";
//echo "enter";die;
/* Check if previous file exists */
$chkRs = $this->db->select('image')->where('id', $this->session->userdata['logged_user']['id'])->get('admins');
//echo $this->db->last_query();die;
if($chkRs->num_rows() > 0){
$chkD = $chkRs->row_array();
if($chkD['image'] != ""){
### delete the previous image ###
$pathActual = './profile_images/';
$pathMedium = './profile_images/medium/';
$pathThumb = './profile_images/thumbs/';
if(file_exists($pathActual.$chkD['image'])){ #delete the actual image
unlink($pathActual.$chkD['image']);
}
if(file_exists($pathMedium.$chkD['image'])){ #delete the medium image
unlink($pathMedium.$chkD['image']);
}
if(file_exists($pathThumb.$chkD['image'])){ #delete the thumb image
unlink($pathThumb.$chkD['image']);
}
### delete the previous image ###
}
}
/* Check if previous file exists */
//print_r($_FILES['image']);die;
$this->load->library('image_lib');
$configUpload['upload_path'] = './profile_images/';
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$configUpload['max_size'] = '0';
$configUpload['max_width'] = '0';
$configUpload['max_height'] = '0';
$configUpload['encrypt_name'] = true;
$this->load->library('upload', $configUpload);
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$configThumb['source_image'] = $uploadedDetails['full_path'];
$configThumbMedium['source_image'] = $uploadedDetails['full_path'];
$raw_name = $uploadedDetails['raw_name'];
$file_ext = $uploadedDetails['file_ext'];
$imgname = $raw_name.$file_ext;
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
}
//die();
/* Upload Image */
$data = $this->input->post(null);
//echo "<pre>";print_r($data);echo "</pre>";die;
if(isset($imgname) && $imgname != ""){
$data['image'] = $imgname;
}
$this->db->where('id',$this->session->userdata['logged_user']['id'])->update('admins', $data);
return 1;
}
it's likely you need to pass source_image the full path of the image on the server. so if you're trying to use /uploads, then it's likely you'll need to set it as /var/www/html/uploads or whatever the full path is to that folder since the image processing is relative to the server, not the site url