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.
Related
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!
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');
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...
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.
Am having difficulties uploading images with codeigniter it keeps saying that i didn't select a file to upload.
this is my code
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '3000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()){
echo $this->upload->display_errors();
}else{
echo $this->upload->data();
}
}
it doesn't even send the image to the directory.
try this function instead :
function upload_done() {
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['upload_path'] = '/var/www/uploadfiles/'; // use an absolute path
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '0';
if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload() )
{
echo "UPLOAD ERROR ! ".$this->upload->display_errors();
} else {
echo "THE IMAGE HAS BEEN UPLOADED : "; var_dump( $this->upload->data() );
}
}
Make sure your html looks like below.
<input type="file" name="userfile" size="20" />
Codeigniter's upload library assumes the name to be "userfile"
You need to change the call to do_upload() depending on the name you have given the form input. Lets say you have this code in your HTML:
<input type="file" name="image" />
You need to change:
$this->upload->do_upload()
to
$this->upload->do_upload('image')
By default CodeIgniter assumes the name is "userfile".