Codeigniter upload error - codeigniter

Please help me! I want to upload a file with codeigniter, but this return to error. This is the uploader:
class Uploader extends CI_Model
{
function __construct()
{
parent::__construct();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
$config['max_size'] = '8192'; // kbytes
$config['encrypt_name'] = TRUE;
$this->load->library( 'upload', $config );
$this->response = '';
$this->success = TRUE;
}
/**
* Triggers upload
* returns the file's details on success or false
*/
function upload( $field_name )
{
if ( !$this->upload->do_upload( $field_name ) )
{
$this->success = FALSE;
$this->response = $this->upload->display_errors();
return false;
}
else
{
$this->success = TRUE;
$this->response = $this->upload->data();
return $this->response['file_name'];
}
}
}
And this is the error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Downloads::$required_empty
Filename: core/Model.php
Line Number: 51
The autoload database config is on. Please help me.

try this
if($this->input->post())
{
$file_element_name = 'image'; //this is the name of your input file. for example "image"
if ($_FILES['image']['name']!= "")
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
$config['max_size'] = '8192';
$config['remove_spaces']=TRUE; //it will remove all spaces
$config['encrypt_name']=TRUE; //it wil encrypte the original file name
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error',$error['error']);
redirect('controller_name/function_name','refresh');
}
else
{
$data = $this->upload->data();
return $data['file_name'];
}
$this->session->set_flashdata('msg','success message');
redirect('controller_name/function_name','refresh');
}
else
{
//if no file uploaded the do stuff here
}
}
please let me know if you face any problem.

have you tried something like this
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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);
}
}
}
?>
you can get more here http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
The Upload Folder
You'll need a destination folder for your uploaded images. Create a
folder at the root of your CodeIgniter installation called uploads and
set its file permissions to 777.

Related

how to upload image and insert into database into codeigniter 3

i try to upload image and insert it into database but it's not working.
there is no any error showing in page and image is also not uploading or inserting into database,
please, give me a solution for that how can i solved this problem?
my code is here,
the code of controller is here, enter code here
public function add_product()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$input_name = "product_image";
if (!$this->upload->do_upload($input_name))
{
$data['items'] = $this->data_model->get_data_info('product_id','product');
$this->load->view('product',$data);
}
else
{
$image_info = $this->upload->data();
$data = array(
'product_name'=>$this->input->post('product'),
'product_pic'=>$image_info['file_name'],
'description'=>$this->input->post('description')
);
$query = $this->data_model->add_data($data,'product');
if($query == TRUE){
echo "product added";
$data['items'] = $this->data_model->get_data_info('product_id','product');
$this->load->view('product',$data);
}else{
echo"product already exists";
$data['items'] = $this->data_model->get_data_info('product_id','category');
$this->load->view('product',$data);
}
}
}
model :
public function add_data($data,$table){
$result = $this->db->get_where($table,$data);
$query = (bool)$result->num_rows();
if(!$query){
if($this->db->insert($table,$data)){
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
controller code
function add_product() {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($_FILES['product_image'])) {
$config['upload_path'] = './Uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $_FILES['product_image']['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('product_image')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload('product_image')) {
$image_data = $this->upload->data();
$full_path = $config['file_name'];
$data["product_pic"] = $full_path;
$query = $this->data_model->add_data($data, 'product');
if ($res == TRUE) {
echo "product added";
$data['items'] = $this->data_model->get_data_info('product_id', 'product');
$this->load->view('product', $data);
} else {
echo"product already exists";
$data['items'] = $this->data_model->get_data_info('product_id', 'category');
$this->load->view('product', $data);
}
}
}
}
$this->load->view('product');
}
model code
function add_data($data, $table) {
if ($query = $this->db->insert($table,$data)) {
$insert_id = $this->db->insert_id();
return $insert_id;
} else {
return false;
}
}

Codeigniter news image resize

How to resize the news picture? I never could.
My problem [source_image]
I use Codeigniter 3.1.6
Controller
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('news_image'))
{
$image = $this->upload->data();
$image_url = $image['file_name'];
$db_insert ='upload/news/'.$image_url.'';
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $db_insert,
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
echo "yes";
} else {
echo "no";
}
}
}
You can try this solution for your problem :
Controller :
<?php
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20240000245';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
// You can change width & height
$imgage_width= 60;
$imgage_height= 60;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
$error = $this->upload->display_errors();
// uploading failed. $error will holds the errors.
$this->form_validation->set_message('error',$error);
return FALSE;
} else {
$fdata = $this->upload->data();
$configer = array(
'image_library' => 'gd2',
'source_image' => $fdata['full_path'],
'maintain_ratio' => TRUE,
'width' => $imgage_width,
'height' => $imgage_height,
);
// Load Image Lib Library
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$img_data ['path'] = $config['upload_path'] . $fdata['file_name'];
// uploading successfull, now do your further actions
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $img_data ['path'],
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
$this->form_validation->set_message('success',"News has been added successfully.");
redirect('contorller/action_name');
} else {
$this->form_validation->set_message('error',"Error in aading news");
redirect('contorller/action_name');
}
}
}
?>
I Hope it will Help you.

how to do image upload in codeigniter?

I am trying to upload an image in root folder and its file name in database. here is what I did for the upload function:
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
print_r($picture); exit;
}
}
print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
problem here is i am being able to insert other data except image. I get the image name with that print_r($config[file_name]) if i do print_r() and exit, if not it will just insert other data except image. But the image is neither uploaded in root folder nor its name in database. If I give the non existing upload path, then also its not throwing any error. I think code inside If is not executed. How can i solve this ? Thanks in advance.
private function _upload_image( ) {
$this->load->library( 'upload' );
if ($_FILES && $_FILES['picture']['name'] !== ""){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|bmp';
$config['max_size'] = 10000;
/*the picture name must be unique, use function now()*/
$config['file_name'] = $_FILES['picture']['name'] . now();
$config['file_ext_tolower'] = TRUE;
$this->upload->initialize( $config );
if ( $this->upload->do_upload( 'picture' ) ){
$file_name = $this->upload->data()['file_name'];
$full_path = $this->upload->data()['full_path'];
/*If you want create a thumb, use this part*/
$this->load->library('image_lib');
$config = array(
'source_image' => $path,
'new_image' => $this->_image_path,
'maintain_ratio' => true,
'width' => 128,
'height' => 128,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
);
$this->image_lib->initialize( $config );
$this->image_lib->resize();
/*Save in database*/
$this->db->insert('blog', [
'file_name' => $file_name,
'full_path' => $full_path
]);
} else {
//if picture is empty, do something
}
}
}
You do not need to use $_FILES && $_FILES ['picture']['name']! == "" only if your form has the picture field as an optional field, $this->upload->do_upload('picture') and get data from $this->upload->data(), read the manual
public function add_blog()
{
$config['upload_path'] = '.uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('picture'))
{//Do something with errors
$errors = $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$this->db->insert('blog', [
'file_name' => $data['file_name'],
'full_path' => $data['full_path']
]);
}
}
I just didn't mention the file size to be uploaded. I did this in my above code and worked.
EDIT
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
// print_r($picture); exit;
}
}
// print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
And this code works for both insert and update.

Invalid argument supplied for foreach()

I do not understand how to fix this codes:
return $this->db->update('slideshow');
I have tried several codes like changing it to $query or $query_result etc. and the error still exist. I still have to use $this->db->update('');
controllers/Cuploadfile.php
function uploadedit()
{
$data['images'] = ''; // Need to place it here and get images here.
//set preferences
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|gif';
$config['max_size'] = '500000';
//load upload class library
$this->load->library('upload', $config);
$this->load->model('Mpages');
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('addslideshows', $upload_error);
}
else
{
// case - success
$upload_data = $this->upload->data();
$filename = $upload_data['file_name'];
$image_id = $this->uri->segment(3);
$data['images'] = $this->Mpages->edit_slideshow($filename, $image_id);
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('editslideshows', $data);
}
}
models/Mpages.php
public function edit_slideshow($filename, $image_id)
{
$this->db->set('slideshow_image', $filename);
$this->db->where('image_id', $image_id);
return $this->db->update('slideshow');
}
Change your Model's function to this
public function edit_slideshow($filename, $image_id){
$this->db->where('image_id', $image_id);
$this->db->update('slideshow', $filename);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
}
rewrite your model
public function edit_slideshow($filename, $image_id)
{
$this->db->set('slideshow_image', $filename);
$this->db->where('image_id', $image_id);
$update=$this->db->update('slideshow');
if($update)
{
return true;
}
return false;
}

Severity: Notice Message: Undefined variable: img when trying to upload to server

I'm trying to update my information: In the localhost it is not error variable: img, but when i upload it into the server it is error like that A PHP Error was encountered:
Severity: Notice Message: Undefined variable: img Filename: controllers/pages.php Line Number: 79....
function update_ads($id)
{
if($this->input->post('update_ads'))
{
$this->db->select('img');
$this->db->where('id',$id);
$query = $this->db->get('tblp_label_slides',$id);
foreach($query->result() as $row)
{
$img = $row->img;
$image = $this->session->set_userdata('img', $img);
}
if($img!='')
{
$config['upload_path'] ='./images/ads';
$config['allowed_types'] ='jpg|jpeg|gif|png';
$config['max_size'] = 1024*8;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('imgfile'))
{
$edit['update'] = $this->mod_write->up_adv($this->session->userdata('img'), $id);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
else
{
$img_content = $this->upload->data('imgfile');
$edit['update'] = $this->mod_write->up_adv($img_content['file_name'], $id);
unlink("./images/ads/".$img);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
}
else
{
$config['upload_path'] = './images/ads';
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 1024*8;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
$this->load->library('upload', $config);
if(! $this->upload->do_upload('imgfile'))
{
$edit['update'] = $this->mod_write->up_adv('', $id);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
else
{
$img_content = $this->upload->data('imgfile');
$edit['update'] = $this->mod_write->up_adv($img_content['file_name'], $id);
unlink("./images/ads/".$img);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
}
}
}
my model
function up_adv($img_content)
{
$id = $this->uri->segment(3);
$data = array(
'dates'=>date('Y-m-d H:i:s'),
'url'=>replace_tag($this->input->post('url')),
'title'=>replace_tag($this->input->post('title')),
'img'=>$img_content,
'type'=>$this->input->post('type')
);
$this->db->where('id',pde($id))
->update('tblp_label_slides',$data);
}
I could be wrong but I think the issue how you are validating $img.
Change if ($img != '')
To if (isset($img) && trim($img) !== '')
This will stop an error being thrown if $img has not been declared which will happen if your query doesn't return any rows.
Hope this helps!

Resources