0

i have a problem with uploading files. I am using codeigniter framework and dropzone.js to uplaod the files.

in my view:

<!-- START MODAL ADD FILE -->
   <!-- Modal -->
    <div class="modal fade" id="myModalFile" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Nahrát Soubory</h4>
          </div>
          <div class="modal-body">           
                 <form action="<?php echo base_url("items/upload_agreement"); ?>" class="dropzone" method="POST" enctype="multipart/form-data">
                      <input type="hidden" name="agreement_dir_name" value="<?= $agreementDir;?>"  >
                      <input type="hidden" name="dir_name" value="<?= $customer_dir;?>"  >
                      <input type="hidden" name="customer_id" value="<?= $customerID;?>"  >
                      <input type="hidden" name="agreement_id" value="<?= $agreementID;?>"  > 
                    <div class="fallback">
                      <input name="fileToUpload[]" type="file" id="fileToUpload" multiple />
                    </div>               
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-danger btn-default" data-dismiss="modal">Zavřít</button>
            <input type="submit" class="btn btn-success btn-default" value="Nahrát" >
            </form>
          </div>

        </div>
      </div>
    </div>
<!-- END MODAL ADD FILE -->

controller:

public function upload_agreement()
{
$customerDir = $this->input->post('dir_name');
$agreementDir = $this->input->post('agreement_dir_name');

$config['upload_path']          = "/www/Cesarlease2/leases/$customerDir/$agreementDir/";


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

                $data = array('upload_data' => $this->upload->data());
                $this->session->set_flashdata('success_msg', 'Soubor úspěšně nahrán');
                redirect("items/items/".$this->input->post('customer_id')."/".$this->input->post('agreement_id'));

}

The problem is that uploaded file is returning as empty:

array(1) { ["upload_data"]=> array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(56) "/www/Cesarlease2/leases/testircrswqe789745asda/2-42017a/" ["full_path"]=> string(56) "/www/Cesarlease2/leases/testircrswqe789745asda/2-42017a/" ["raw_name"]=> bool(false) ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" } }

I tried dumping the POST value of fileToUpload input, also empty. Is there something i am overlooking ?

2
  • pls read carefully this : codeigniter.com/user_guide/libraries/file_uploading.html Commented Jul 20, 2018 at 11:44
  • 1
    Hi, you are not actually processing the actual upload here. You need to call $this->upload->do_upload('fileToUpload[]'); before you can see data in $this->upload->data() - this is a slight bit trickier with multiple file uploads as you will have to give each file upload a unique name and then loop over the $_FILES super global to process each one with CodeIgniter Commented Jul 20, 2018 at 13:47

3 Answers 3

1

When using codeigniter upload library it by default expects a Post variable named userfile and a miltipart form.

You can open a miltipart form with this code

<?php echo form_open_multipart('controller/method');?>

This may substitute your <form> tag and you can close it with this code

<?php echo form_close(); ?>

Or you can keep using your </form> tag

In your controller, since you are using a different post variable name and an array you should code a loop something like

$i=0
foreach ($this->input-post('fileToUpload') as $p ){
    $data[$i] = array('upload_data' =>$this->upload->data($p));
    $i++;
}

This may work, also you may want to avoid full paths like the one in your config path, that will be very difficult to change on deployment and you can have a lot of errors, instead you can use something like <?php base_url('anyFolderInYourProject/anySubfolder'); ?> which gives you the complete path of your project directory and adds whatever route your put inside.

Hope this helps, regards.

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

2 Comments

I had to throw away dropzone for now and currently i am using simple multifile upload, for some reason <div class="fallback"> was the first problem. Second problem was in the path itself as you mentioned. Edited the path to $config['upload_path'] = "leases/$customerDir/$agreementDir/" and this resolved the issue with upload.
0

You don't have to send multiple files at once. In fact, this isn't the default behavior: http://www.dropzonejs.com/#config-uploadMultiple. Dropzone can process multiple files simultaneously through different requests by hitting the same upload url via parallelUploads. This is what I use, and it greatly reduces the complexity of your backend code at no extra cost. So in essence, loose the array and set parallelUploads to whatever you want.

You also need to use the function do_upload() for uploads to actually process. See the CI docs for more on this. It is pretty straightforward.

Comments

-2

You can try using this before load->library:

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

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.