8

I'm sure this is simple but I can't see how to make uploading a file with CI optional.

If you leave the file input box empty, the error "You didn't choose an upload file" appears.

The reason I want it to be optional is that my form edits a directory type listing, and I don't need to upload the image each time I edit the listing.

Is there a way to remove the "required" error handling on the file class

4 Answers 4

13

Use the following:

<?php if ( $_FILES AND $_FILES['field_name']['name'] ) 
{
    // Upload the file
}
Sign up to request clarification or add additional context in comments.

2 Comments

use it where? why is it so hard to type a little bit more to explain where it goes...
I am sure in controller, before codeigniters upload rutine is called.
4

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 ....

2 Comments

Can you explain the significance of 4 and where you got this from?
$phpFileUploadErrors = array( 0 => 'There is no error, the file uploaded with success', 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 3 => 'The uploaded file was only partially uploaded', 4 => 'No file was uploaded', 6 => 'Missing a temporary folder', 7 => 'Failed to write file to disk.', 8 => 'A PHP extension stopped the file upload.', ); From: php.net/manual/en/features.file-upload.errors.php
0

Use this code in the controller before calling do_upload()

if (is_uploaded_file($_FILES['field_name']['tmp_name'])) {
    // your code here
}

Comments

0

Use This Code :-

$config['upload_path'] = 'assets/img/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload', $config);
    // Upload the file
        if ($this->upload->do_upload('Image')){
            $dataimage = $this->upload->data();
                $data = array(
                   'image' => $dataimage['file_name'],
                   'UserName' => $this->input->post('UserName'),
                   'Password' => $this->input->post('Password'),
                   'xid' => $this->input->post('xid')
                );
        }
        else{
               /*$out['msg'] = show_err_msg($this->upload->display_errors());
               echo json_encode($out);
               exit();*/
                $data = array(
                   'image' => NULL,
                   'UserName' => $this->input->post('UserName'),
                   'Password' => $this->input->post('Password'),
                   'xid' => $this->input->post('xid')
                );
        }

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.