1

I looked for this in Google and different answers on stackoverflow. And probaly there is an good answer in them but still i don't get how i can implent it in my own code.

I got my own public function to upload an image, but now i want it to be optional. At this moment someone needs to upload an file to pass the validation, how can i make this optional?

my function:

public function _do_upload_image()
{
    $config['upload_path'] = './company_images/';
    $config['allowed_types'] = 'jpg|jpeg|png';

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
    {
        $this->form_validation->set_message('_do_upload_image', $this->upload->display_errors());
    }
    else
    {
        $this->_upload_data = $this->upload->data();
    }

}

Thanks in advance

--edit--

For other people, the answer worked and i gave my file_name an other name when there was no image uploaded. It looks like this:

public function _do_upload_image()
{
    $config['upload_path'] = './company_images/';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $returnArr = array();

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
    {
        $returnArr = array('file_name' => 'leeg.jpg');
    }
    else
    {
        $returnArr = $this->upload->data();
    }

    $this->_upload_data = $returnArr;
}

2 Answers 2

1

If you mean you need to make your upload function optional then you can just do:


public function _do_upload_image()
{
    $config['upload_path'] = './company_images/';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $returnArr = array(); 

    $this->load->library('upload', $config);

    if ($this->upload->do_upload())
    {
        $returnArr  = $this->upload->data();
    }
    return $returnArr; //just return the array, empty if no upload, file data if uploaded
}

Hope that makes sense

Sign up to request clarification or add additional context in comments.

2 Comments

That works, but is there a way that if no image is uploaded i can give ['file_name'] the value: leeg.jpg ?
Uhm nvm, I got the problem fixed with all the functions i wanted. Ill update my post!
0

codeigniter file upload optionally ...works perfect..... :)

---------- controller ---------

function file()
{
 $this->load->view('includes/template', $data);
}

function valid_file()
{
 $this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean');

 if ($this->form_validation->run()==FALSE) 
 {
    $this->file();
 }
 else
 {
  $config['upload_path']   = './documents/';
  $config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf';
  $config['max_size']      = '1000';
  $config['max_width']     = '1024';
  $config['max_height']    = '768';

  $this->load->library('upload', $config);

  if ( !$this->upload->do_upload('userfile',FALSE))
  {
    $this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors());

    if($_FILES['userfile']['error'] != 4)
    {
        return false;
    }

  }
  else
  {
    return true;
  }
}

i just use this lines which makes it optionally,

if($_FILES['userfile']['error'] != 4)
{
 return false;
}

$_FILES['userfile']['error'] != 4 is for file required to upload.

you can u make it unneccessory by using $_FILES['userfile']['error'] != 4 , then it will pass this error for file required and works great with other types of errors if any by using return false , hope it works for u ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.