can a custom helper extend library in codeigniter - codeigniter

I am trying to make a helper for Image Uploading. Can I extend the library inside a custom helper? I had try to use it and it never works.
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
function FileUploadd($r)
{
$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($r))
{
echo $this->upload->display_errors();
}
else
{
$Dder=$this->upload->data();
return $Dder['file_name'];
}
}

I see you want to call the library inside your helper and not to extend it. You should call get_instance() first because $this only works oncontrollers and models. If you want to use a helper or library you need to call the get Codeigniter's instance first
Your code should look like this:
class Your_helper_name{
private $CI;
function __construct(){
$this->CI =& get_instance();
}
function FileUploadd($r) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->CI->load->library('upload', $config);
if ( ! $this->CI->upload->do_upload($r))
{
echo $this->CI->upload->display_errors();
}
else
{
$Dder=$this->CI->upload->data();
return $Dder['file_name'];
}
}
}
Let me know if you need clarification. If this answers your question, please mark this as answer. Thanks!

Related

how to get upload file name in codeigniter

Hi i'm trying to upload image to a folder and image name in database
in view
echo form_open_multipart('subscribers_c/actioncreate',array('class'=>'form'));`enter code here`
<input type='file' name='img'>
in controller
$config['upload_path'] ='./assets/uploads/subscribers_photos'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config); //Load the upload CI library
if (!$this->upload->do_upload('img'))
{
$uploadError = array('upload_error' => $this->upload->display_errors());
//$this->set_flashdata('msg_error', $uploadError, site_url().'/subscribers_c/actioncreate');
}
$file_info = $this->upload->data('img');
$img_name = $file_info['file_name'];
$data=array(
'chit_fund_id'=>$this->input->post('cid'),
'first_name'=>$this->input->post('fname'),
'last_name'=>$this->input->post('lname'),
'dob'=>$this->input->post('bd'),
'gender'=>$this->input->post('g'),
'contact_number'=>$this->input->post('mob'),
'address'=>$this->input->post('add'),
'email_id'=>$this->input->post('eml'),
'bid_status'=>$this->input->post('b_status'),
'user_status'=>$this->input->post('u_status'),
'image_name'=>$img_name,
);
//print_r($data);
image_name is not having any value
how to get image name .
Instead of img in upload data
$file_info = $this->upload->data('img');
Try
$file_info = $this->upload->data();
$img = $file_info['file_name'];
And on here $config['upload_path'] = './assets/uploads/subscribers_photos'; end with /
Like $config['upload_path'] = './assets/uploads/subscribers_photos/';
Inside the view call the form_upload function(if needed, your one is also correct):
<?php echo form_upload('pic'); ?>
Inside controller function for upload:
$img_name = $_FILES["pic"]["name"];
By using this code you will get the file name.
$img_name = $this->upload->data('file_name');

CodeIgniter renaming image to be the next id in db table

I'm brand new to codeigniter and am trying to rename my image to be the next auto incremented number in the table.
I was looking into using
$id = $this->db->insert_id();
But am still unsure how to add 1 to that value and use it for my file name. I've got image resizing all set just need to rename and I'm set.
Any help would be much appreciated!!! Thanks so much!
Controller:
$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());
redirect('index.php/success');
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
redirect('index.php/success');
echo $img;
}
// Update Record to save filename
}
function resize($path, $file){
$config['image_library']= 'gd2';
$config['source_image']= $path;
$config['create_thumb']= TRUE;
$config['maintain_ration']= TRUE;
$config['width']= 320;
$config['height']= 196;
$config['new_image']='./uploads/'.$file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
Get the last number of the id column which you have set to auto increment.You can use sql Max() function to that.Then simply add a 1 to that and use it for the image renaming.That's it!!!

How can I offer to upload a file without making it required

What I'm doing is a form and part of the thing that you can upload is a file:
<input type="file" name="userfile" size="20" />
It works fine but the only problem is that if the user doesn't select a file the program crashes. I tried to make it not required by adding an if instruction in the controller:
if ($this->input->post('userfile')) {
$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);
$imagen = $this->upload->do_upload();
}
But this lousy attempt doesn't seem to work because $this->input->post('userfile') doesn't contain anything, regardless if the user selects a file or not.
So the question is: How can I know if the user selected a file (or not) so that I can properly handle it on my controller?
in PHP, <input> elements with the type="file" will automatically populate the PHP array $_FILES instead of $_POST, which codeigniter's $this->input->post() function looks in.
http://php.net/manual/en/reserved.variables.files.php
Therefore, You can check if the user has uploaded any files by doing the following:
if($_FILES['userfile']['error'] == UPLOAD_ERR_OK)
{
// A file was uploaded
$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);
$imagen = $this->upload->do_upload();
}
if($_FILES['userfile']['error'] == UPLOAD_ERR_OK)
{
// A file was selected and uploaded to the server for further processing
}
Other possible values for the error field if you need to give more extensive feedback.

upload size limit in codeigniter

I have a page where user can register. He uploads a profile picture in the process. I want to limit the size but there is not much emphasis on codeigniter documentation except for $config['maxsize']. I tried the following but I don't get any messages. I set the size to 10 (KB) just for testing. Do I have to handle it somehow to get the message across to my view?
public function add_user()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
$fullImagePath;
if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name']))
{
if ($this->upload->do_upload('userfile'))
{
// set a $_POST value for 'image' that we can use later
...
By the way this code is in my Model.
#Chetan Wadhwa,
Are you sure the size is in Bytes(Not in KB). Refer codeigniter documentation: http://www.codeigniter.com/user_guide/libraries/file_uploading.html, must be in KB.
This Size is not in KB it is in Bytes so for 10KB you should write (10*1024) Bytes or "10240"
You don't need to handle the error message CI will take care that in this case so please follow the upload class tutorial which specified in codeIgniter userguide.
<?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);
}
}
}
?>

file upload code igniter..not working

This is in my controller for file upload
$config['upload_path'] = './assets/images/b2b/banner-agent/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = "$banner2";
$this->load->library('upload', $config);
$this->upload->data();
$this->upload->do_upload();
$this->upload->initialize($config);
is there any wrong with my code? The upload not working.
You can not simply call do_upload method before initializing and setting config variables for the upload class.
You need to modify your code like this:
$config['upload_path'] = './assets/images/b2b/banner-agent/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = $banner2;
$this->load->library('upload'); //initialize
$this->upload->initialize($config); //Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class
$this->upload->do_upload(); // do upload
if($this->upload->do_upload()){
$this->upload->data(); //returns an array containing all of the data related to the file you uploaded.
}
You can consult Codeigniter wiki for that too:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
Hope this helps.

Resources