ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Multiple file upload library

January 06, 2012 7:36am

Subscribe [16]
  • #1 / Jan 06, 2012 7:36am

    Romyblack

    24 posts

    Hi Everyone.

    I’m new at this and am a fan of this incredible CodeIgniter Framework,
    here i share with you this libraries to upload multiple files, it have been
    documented for easy integration.

    I hope this is useful for you.

    /*UPDATED 6/9/2012
    I updated the library according to your need now you can upload with file arrays or normal file inputs as well, hope you enjoy. If you find this library helpful please say thanks and share it with people you know need this.
    ***********************************
    /*UPDATED 21/10/2012
    Hi everybody, i modified this library as you requested, first at all.
    Special thanks to frankabel and Pent to help me improve this library, ok
    so now the problem with the appended texts of error was resolved and now, the array that
    the library returns has a new attribute called [FILE_INPUT] which tells you from which input field the file was submitted or selected.

    Example Code

    'file_name' => string 'NCF_BLOQUEADOS.txt' (length=18)
    'file_type' => string 'text/plain' (length=10)
    'file_path' => string 'C:/wamp/www/code/uploads/' (length=25)
    'full_path' => string 'C:/wamp/www/code/uploads/NCF_BLOQUEADOS.txt' (length=43)
    'raw_name' => string 'NCF_BLOQUEADOS' (length=14)
    'orig_name' => string '' (length=0)
    'client_name' => string 'NCF_BLOQUEADOS.txt' (length=18)
    'file_ext' => string '.txt' (length=4)
    'file_size' => float 1199.88
    'is_image' => boolean false
    'image_width' => string '' (length=0)
    'image_height' => string '' (length=0)
    'image_type' => string '' (length=0)
    'image_size_str' => string '' (length=0)
    'error_msg' => string 'El archivo que está tratando de subir excede el tamaño permitido' (length=73)
    'file_input' => string 'file' (length=4)

    I hope you enjoy.

    Cordially,
    Romyblack.

    Save this with this name (MY_Upload.php)
    Click here: DOWNLOAD MY_Upload.php

    PayPal library released 😊 see post <here>

  • #2 / Jan 19, 2012 5:56pm

    mattsmith

    2 posts

    Thanks a bunch! This saved me like an hour of obnoxious work.
    I did get a vague permissions error when trying to create the index.php files(but I didn’t need those).

  • #3 / Jan 20, 2012 1:26pm

    Romyblack

    24 posts

    Thanks a bunch! This saved me like an hour of obnoxious work.
    I did get a vague permissions error when trying to create the index.php files(but I didn’t need those).

    You’re welcome, you can modify the library to your needs, the problem you’re having is because of the file’s permission.

    In the console try this.

    if the folder where you want the files to be uploaded is downloads

    chmod +rw downloads

    The script will have permissions to write a file on it 😊

    Hope this help.

  • #4 / Jan 20, 2012 5:57pm

    mattsmith

    2 posts

    No, the upload directory was already set to 777. If that were the problem, it wouldn’t be able to upload the files themselves.

  • #5 / Jan 21, 2012 6:18pm

    sony144

    1 posts

    Huge help thanks for this.

  • #6 / Jan 27, 2012 9:45pm

    Romyblack

    24 posts

    Huge help thanks for this.

    No problem, its a pleasure to help you with this little thing.

    I feel very good 😊

  • #7 / Feb 06, 2012 10:42am

    pierrest

    2 posts

    Hello everybody,

    This library was workin perfect on my dev server, but now just deploying my project to the production server and i get this message

    Call to undefined method CI_Upload::up()

    my controller:

    <?php
    
    class Upload extends CI_Controller {
    
     function __construct()
     {
      parent::__construct();
      $this->load->helper(array('form', 'url'));
     }
    
     function index()
     {
      $this->load->view('admin/modify_module', array('error' => ' ' ));
      
      $this->load->library('session');
      $this->load->library('encrypt');
      $this->data['username'] = $this->session->userdata('user');
     }
     function do_upload_member() {
       $this->load->library('upload');
    
       $config['upload_path']   = './uploads'; //if the files does not exist it'll be created
       $config['allowed_types'] = 'gif|jpg|png|xls|xlsx|php|pdf';
       $config['max_size']   = '4000'; //size in kilobytes
       $config['encrypt_name']  = TRUE;
    
       $this->upload->initialize($config);
    
       $uploaded = $this->upload->up(TRUE); //Pass true if you want to create the index.php files
    
      $this->load->model('members');
       if ($this->input->post('submit')) {
        $this->members->add_member($uploaded); 
       }
      $data['pagetitle'] = " Adhésion";
      $data['pageref'] = 'Nous avons bien reçu votre demande'; 
      $this->load->view('head', $data);
      $this->load->view('confirmation');
     }
     
    
     
    }
    ?>

    have been for a while on it and i am lost… 

    i have some rewriting htaccess stuff too which was not on the dev server:
    (there is an evil joomla project on the same sever (which i did not develop myself) i use a subdomain for CI)

    php_flag “allow_url_fopen” “On”
    php_flag “allow_url_include” “On”

    #AuthType Basic
    #AuthName “Le site est en maintenance, veuillez repasser plus tard”
    #AuthUserFile /home/www/c5f672d79af39528e95ff68c3587e35e/web/members/.htpasswd

    #<Files index.php>
    #require valid-user
    #</Files>

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]


    is it this rewrite which may causing the problem?

    Thanks a lot if you can help me

  • #8 / Feb 06, 2012 10:59am

    Romyblack

    24 posts

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]


    is it this rewrite which may causing the problem?

    Thanks a lot if you can help me

    Hi pierrest, please verify that you paste the library in the correct folder and named like this
    MY_Upload.php, remember that when you put this library in your library folder, (not the core’s library folder), it calls it automatically and override the functions with the same name.

    Another thing, to confirm if your .htaccess is the problem, in your upload controller, call another library to see if it works property, if not then the problem is the .htaccess. if yes we’d better find another solution.

    let us know the results.

  • #9 / Feb 06, 2012 4:22pm

    InsiteFX's avatar

    InsiteFX

    6819 posts

    Some servers are case sensitive and need all filenames in lower case!

  • #10 / Feb 07, 2012 8:20am

    pierrest

    2 posts

    File was named MY_upload and not MY_Upload…. :red:

    It works now, library found, but it return nothing => bool(false)

    ...  but the files are uploaded at the right place

    EDIT: The problem was me again…=>  i commented the whole if condition at line 113 of MY_Upload (#Here we do the upload process) to let the upload optional.


    sorry to disturb for such things….

  • #11 / Mar 28, 2012 1:31pm

    Nihilistik

    1 posts

    Hi there!

    I’m trying to learn CodeIgniter, and there is no better way than find something that you need and there isn’t any solution that match to your needs.

    So, in this case i need to upload files through a multiple input file, what, as you know, require an “array name” like

    name="files[]"

    and a multiple attribute

    multiple="multiple"

    I don’t know if there is any issue about this attribute, let me know if it is. My solution for this way to upload images was based on Romyblack solution… thanks for it!

    I’m gonna post the modified code only:

    My view form (Edited for correction, thx Romyblack)

    <form method="POST" action="" enctype="multipart/form-data">
        <input type="file" name="files[]" size="20" multiple="multiple"/>  
        <input type="submit" name="test" value="TEST" />
     </form>

    Modified library

    public function up($protect = FALSE){
    
      /*
      * Declare uploaded_info and uploaded_files
      * when i'm sure $_FILES has some data
      */
      if($this->upload_path[strlen($this->upload_path)-1] != '/')
       $this->upload_path .= '/';
     
      
      if(isset($_FILES)){
      
       #Here we check if the path exists if not then create
       if(!file_exists($this->upload_path)){
        @mkdir($this->upload_path,0700,TRUE);
       }
        $uploaded_info  = FALSE;
        /*
        * The structure of $_FILES changes a lot with the array name on the input file,
        * then i'm gonna modify $_FILES to make it think the data comes from several
        * input file instead of one "arrayfied" input.
        *
        * The several ways to upload files are controled with this if…else structure
        */
        if(count($_FILES) == 1)
        {
            $main_key = key($_FILES);
            if(is_array($_FILES[$main_key]['name']))
            {
                
                foreach($_FILES[$main_key] as $key => $value)
                {                
                    
                    for($y = 0; $y < count($value); $y++)
                    {
                        
                        $_FILES[$main_key .'-'. $y][$key] = $value[$y];
                            
                    }
                    
                    
                }
                
                unset($_FILES[$main_key]);
                
                $uploaded_files  = $_FILES;
            }
            else
            {
                $uploaded_files  = $_FILES;    
            }
            
        }
        else
        {
            $uploaded_files  = $_FILES;    
        }
        
       #Here we create the index file in each path's directory
       if($protect){
        $folder = '';
        foreach(explode('/',$this->upload_path)  as $f){
         
         $folder .= $f.'/';
         $text = "<?php echo 'Directory access is forbidden.'; ?>";
         
         if(!file_exists($folder.'index.php')){
          $index = $folder.'index.php'; 
          $Handle = fopen($index, 'w');
          fwrite($Handle, trim($text));
          fclose($Handle); 
         }
        }   
       }
    
       #Here we do the upload process
         
       foreach($uploaded_files as $file => $value){
        if (!$this->do_upload($file))
        {
         $uploaded_info['error'][]  =  array_merge($this->data(),
                  array('error_msg' => $this->display_errors()));
         
        }
        else
        {
         $uploaded_info['success'][] =  array_merge($this->data(),
                  array('error_msg' => $this->display_errors()));
        }
       }  
      }
      
      #Then return what happened with the files
      return $uploaded_info;
     }

    It’s working in my project, i hope this will be useful. The usage it’s the same that
    Romyblack explain at the begining of his library.

    Let me know if there is any kind of issue or style violation.

     

  • #12 / Mar 28, 2012 11:35pm

    Romyblack

    24 posts

    Hi there!

    Let me know if there is any kind of issue or style violation.

    Hi, Nihilistik!

    You have this in your view file

    <form method="POST" action="" enctype="multipart/form-data">
        <input type="files[]" name="file_1" size="20" multiple="multiple"/>  
        <input type="submit" name="test" value="TEST" />
     </form>

    But it should be like this according to the w3s documentation

    <form method="POST" action="" enctype="multipart/form-data">
        <input type="file" name="files[]" size="20" multiple="multiple"/>  
        <input type="submit" name="test" value="TEST" />
     </form>

    I really appreciate that this library its useful to other people, please, be free to make any modifications according to your needs,.

  • #13 / Mar 29, 2012 7:01am

    karlis_i

    4 posts

    Modified this great library to enable optional file uploads ( no more “You did not select a file to upload.” messages )-
    What you need to do is wrap upload process in “if” block at line 115:

    replace

    #Here we do the upload process
    foreach ($uploaded_files as $file => $value) {
        if (!$this->do_upload($file)) {
            $uploaded_info['error'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
        } else {
            $uploaded_info['success'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
        }
    }

    with

    #Here we do the upload process
    foreach ($uploaded_files as $file => $value) {
        if( $value['size'] > 0 ){
            if (!$this->do_upload($file)) {
                $uploaded_info['error'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
            } else {
                $uploaded_info['success'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
            }
        }
    }
  • #14 / May 18, 2012 3:15pm

    Romyblack

    24 posts

    Modified this great library to enable optional file uploads ( no more “You did not select a file to upload.” messages )-
    What you need to do is wrap upload process in “if” block at line 115:

    replace

    #Here we do the upload process
    foreach ($uploaded_files as $file => $value) {
        if (!$this->do_upload($file)) {
            $uploaded_info['error'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
        } else {
            $uploaded_info['success'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
        }
    }

    with

    #Here we do the upload process
    foreach ($uploaded_files as $file => $value) {
        if( $value['size'] > 0 ){
            if (!$this->do_upload($file)) {
                $uploaded_info['error'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
            } else {
                $uploaded_info['success'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
            }
        }
    }

    Thanks for your contribution karlis_i

  • #15 / May 23, 2012 11:32am

    HunterJoe1977

    3 posts

    Modified this great library to enable optional file uploads ( no more “You did not select a file to upload.” messages )-
    What you need to do is wrap upload process in “if” block at line 115:

    replace

    #Here we do the upload process
    foreach ($uploaded_files as $file => $value) {
        if (!$this->do_upload($file)) {
            $uploaded_info['error'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
        } else {
            $uploaded_info['success'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
        }
    }

    with

    #Here we do the upload process
    foreach ($uploaded_files as $file => $value) {
        if( $value['size'] > 0 ){
            if (!$this->do_upload($file)) {
                $uploaded_info['error'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
            } else {
                $uploaded_info['success'][] = array_merge($this->data(), array('error_msg' => $this->display_errors()));
            }
        }
    }

    This is perfect! Exactly what I needed. Thank you.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases