1

Let me describe my code first.

View:

<?php echo form_open_multipart('question_edit/update_question'); ?>
  ....
  <div class="form-group">
      <label for="ask_q" class="">A Brief Description of your question <em>(Optional)</em></label>
       <textarea name="ask_q" id="ask_q"><?php echo $uposts->question_desc; ?></textarea>
  </div>
  <div class="form-group">
      <label for="upld" class="">Upload New Docs <em>(Optional)</em></label>
      <input type="file" name="upld[]" id="upld" style="width: 100%;" multiple>
  </div>
  ....
<?php echo form_close(); ?>

Please note that my file input field is not mandatory. It is optional.

Controller:

function update_question(){

    $update_data = array(
        'question_desc'   =>  $this->input->post('ask_q')
    );

    $this->ask_model->update_q($update_data);

    if(!empty($_FILES['upld']['name'])){
        $filesCount = count($_FILES['upld']['name']);
        for($i = 0; $i < $filesCount; $i++){
            $_FILES['userFile']['name'] = $_FILES['upld']['name'][$i];
            $_FILES['userFile']['type'] = $_FILES['upld']['type'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['upld']['tmp_name'][$i];
            $_FILES['userFile']['error'] = $_FILES['upld']['error'][$i];
            $_FILES['userFile']['size'] = $_FILES['upld']['size'][$i];

            $uploadPath = './uploads/';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'gif|jpg|jpeg|png|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|txt';
            $config['max_size'] = '1048576';

            $this->upload->initialize($config);

            if(!$this->upload->do_upload('userFile')){
                $this->session->set_flashdata('q_failure', 'Something went wrong. Please try again.');
                redirect('/user-qa');

            } else {
                $fileData = $this->upload->data();
                $uploadData[$i]['uploaded_file'] = $fileData['file_name'];

            }
        }

        if(!empty($uploadData)){
            //Insert files data into the database
            $insert = $this->ask_model->insert_upload($uploadData);
        }
    }

    $this->session->set_flashdata('q_success', '<div class="alert-message success">
            <i class="icon-ok"></i>
            <p><span>Success</span><br>
            Your question has been updated successfully.</p>
        </div>');
    redirect('/user-dashboard');

}

My controller code describes (?) if file upload field is not empty, then upload file.

But the problem is each time I submit the form, it updates the database because of $this->ask_model->update_q($update_data); but giving the error message Something went wrong. Please try again. that I used for validating the $config.

I checked the log file. Log file states

You did not select a file to upload

It seems, my if(!empty($_FILES['upld']['name'])){ is not working.

UPDATE-1

I tried if(isset($_FILES['upld']) && $_FILES['upld']['size'] > 0){ instead of if(!empty($_FILES['upld']['name'])){ but the result is same.

My question is

  1. Where is the problem ?
  2. How to validate file size and file extension type ?

UPDATE-2

Tried if($_FILES['upld']['name']){ by seeing this stackoverflow Link but it is also not working.

Community
  • 1
  • 1
Raja
  • 772
  • 1
  • 15
  • 38
  • Can you print_r the $_FILES variable to see if you are receiving anything ? – B. Assem Dec 24 '16 at 20:07
  • @B.Assem I tried and the result is `Array ( [0] => )` – Raja Dec 24 '16 at 20:09
  • Well that means that for some reasons, no file was sent to the controller! Did you try this https://www.codeigniter.com/userguide3/libraries/file_uploading.html – B. Assem Dec 24 '16 at 20:13
  • why do you have `name="upld[]" ` why not `name="upld"` ? – B. Assem Dec 24 '16 at 20:14
  • @B.Assem Because it is multiple file input field, not single. Second thing is my `controller` code does not ignore [https://www.codeigniter.com/userguide3/libraries/file_uploading.html](https://www.codeigniter.com/userguide3/libraries/file_uploading.html), nowhere. You can easily see the code above. – Raja Dec 24 '16 at 20:18
  • The pbl is not in your controller (I didn't say that :) ) I just said that for some reasons, your files are not sent. In this post, it is said that it can be a rewrite rule pbl: http://stackoverflow.com/questions/9362587/codeigniter-empty-post-files – B. Assem Dec 24 '16 at 20:23
  • And here it was a file type pbl: http://stackoverflow.com/questions/18129360/files-is-empty-in-codeigniter – B. Assem Dec 24 '16 at 20:23
  • @B.Assem I am using IIS 7.5. There are no rewrite rules that can affect file upload. – Raja Dec 24 '16 at 20:33

1 Answers1

0

the reason is when you submit an empty multiple form, it sends empty array with index 0 so we make sure that the first index is not empty

if(isset($_FILES['files']['name'])&&!empty($_FILES['files']['name'][0])): 
Mohamad Alasly
  • 330
  • 3
  • 17