file upload code igniter..not working - codeigniter

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.

Related

How to remove existing file when uploading new one in codeigniter file uploading?

I am using codeigniter file uploading class to upload files and images in my local folder.I am using the code below, it's working fine.
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|txt|sql';
$config['max_size'] = 8048000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('Emp_details_view', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Now, my question is how to remove previously added file when uploading new one.
You can use unlink() function to delete your previous image
unlink('image-path/image-name');
first save your filename in a database table then when unlinking take that value from database and unlink. here uploads is the foldername .
unlink("uploads/".$val['image']);
$val['image'] contains imagename from db.

can a custom helper extend library in 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!

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');

.blend/.blender file upload using codeigniter

I want to upload .blend or .blender file using codeigniter...
$config['allowed_types'] = 'gif|jpg|jpeg|png|psd|xls|xlsx|xml|doc|docx|csv|mpeg|mpg|mp3|mpga|pdf';
$config['max_size'] = '5024';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
echo 'error';
}else{
print_r($this->upload->data());
}
Which Mimes type i m used for blender file extension...

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.

Resources