0

I am trying to upload multiple files and i want to rename each file with current time. But when i am uploading files, I am getting an Error like

You did not select a file to upload.

I really don't know where i went wrong..

Any help would be greatly appreciated. Thank you..

Here is my controller:

public function upload_form()
{
    //load the helper
    $this->load->helper('form');
    $msg = array('msg' => "");
    if (isset($_FILES['upload_file'])) {
        $class = $this->input->post('class');
        $subject = $this->input->post('subject');
        $topic = $this->input->post('topic');

        $msg = array('msg' => "Upload success!");
        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'pdf|jpg|png|jpeg|gif';
        $this->load->library('upload', $config);
        $files = $_FILES;
        $cpt = sizeof($_FILES['upload_file']['name']);

        if(!$this->upload->do_upload('upload_file')){
            $msg = array('msg' => $this->upload->display_errors());
        }
        else{
            $data = array(
                'class' => $class,
                'subject' => $subject,
                'topic' => $topic,
                'file' => [
                    'file_name' => $_FILES['upload_file']['name']
                ]
            );
            $this->mongo_db->insert('file_upload', $data);
        }

        for ($i = 0; $i < $cpt; $i++) {
            $this->upload->initialize($config);
            $file_name = (microtime(true) * 1000);
            $_FILES['upload_file']['name'] = $file_name;
            $_FILES['upload_file']['type'] = $files['upload_file']['type'][$i];
            $_FILES['upload_file']['tmp_name'] = $files['upload_file']['tmp_name'][$i];
            $_FILES['upload_file']['error'] = $files['upload_file']['error'][$i];
            $_FILES['upload_file']['size'] = $files['upload_file']['size'][$i];
        }
    }

    $this->load->view('worksheets/header');
    $this->load->view('worksheets/upload_form', $msg);
}

VIEW:

 <?php echo $msg; ?>
 <?php echo form_open_multipart('worksheets/upload_form/'); ?>
 <input type="file" name="upload_file[]" multiple="multiple" class="custom-     file-input" size="2000"/>
 <input type="submit" value="Fire" class="upload_submit"/>
 <?php echo form_close(); ?>

2 Answers2

0

try to print outcome of $_FILES['upload_file'] and you will find the error .

parveen
  • 557
  • 3
  • 13
  • It seems like files are not uploading, array (size=5) 'name' => string '14235454000_1' (length=13) 'type' => string 'a' (length=1) 'tmp_name' => string 'C' (length=1) 'error' => null 'size' => null – Bipin Kareparambil Feb 10 '15 at 05:17
  • 1
    In html tag should be like – parveen Feb 10 '15 at 05:19
  • "The filetype you are attempting to upload is not allowed." – Bipin Kareparambil Feb 10 '15 at 05:25
  • But when i am changing config['allowed_types']= ' * ' ; , then its working.!! – Bipin Kareparambil Feb 10 '15 at 05:26
  • Yes you need to define allowed types :) .. http://stackoverflow.com/questions/9815208/codeigniter-the-filetype-you-are-attempting-to-upload-is-not-allowed – parveen Feb 10 '15 at 05:28
  • You checked this http://stackoverflow.com/questions/9815208/codeigniter-the-filetype-you-are-attempting-to-upload-is-not-allowed ? $config["allowed_types"] ="*"; – parveen Feb 10 '15 at 05:34
  • yes. i set $config["allowed_types"] ="*"; when im uploading pdf file, file uploaded, but file in uploaded dir shows that its an unknown file type. i cant open it as a pdf file.. looks like .file extension – Bipin Kareparambil Feb 10 '15 at 05:39
0

Basically the Codeigniter File Upload Library is not meant for multiple file upload using <input type="file" name="upload[]" multiple>

You need multiple file inputs because it takes only the name of the input as a string.

Or you can extend the library for multiple file upload.

You can use this one > https://github.com/stvnthomas/CodeIgniter-Multi-Upload

Gopakumar Gopalan
  • 1,187
  • 14
  • 22