Search
 
SCRIPT & CODE EXAMPLE
 

PHP

upload multiple files in codeigniter

    private function upload_files($path, $title, $files)
    {
        $config = array(
            'upload_path'   => $path,
            'allowed_types' => 'jpg|gif|png',
            'overwrite'     => 1,                       
        );

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

        $images = array();

        foreach ($files['name'] as $key => $image) {
            $_FILES['images[]']['name']= $files['name'][$key];
            $_FILES['images[]']['type']= $files['type'][$key];
            $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
            $_FILES['images[]']['error']= $files['error'][$key];
            $_FILES['images[]']['size']= $files['size'][$key];

            $fileName = $title .'_'. $image;

            $images[] = $fileName;

            $config['file_name'] = $fileName;

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

            if ($this->upload->do_upload('images[]')) {
                $this->upload->data();
            } else {
                return false;
            }
        }

        return $images;
    }
Comment

Multiple image upload with CodeIgniter

views
---------

<?php echo validation_errors();?>
<?php echo form_open_multipart('pages/multiple_files');?>
<p><input type="file" multiple="multiple" name="image_name[]" class="form-control" /></p>

<input type="submit" class="btn btn-success btn-block"/> 
</form>

Controller
----------

public function multiple_files(){
  $this->load->library('upload');
  $image = array();
  $ImageCount = count($_FILES['image_name']['name']);
        for($i = 0; $i < $ImageCount; $i++){
            $_FILES['file']['name']       = $_FILES['image_name']['name'][$i];
            $_FILES['file']['type']       = $_FILES['image_name']['type'][$i];
            $_FILES['file']['tmp_name']   = $_FILES['image_name']['tmp_name'][$i];
            $_FILES['file']['error']      = $_FILES['image_name']['error'][$i];
            $_FILES['file']['size']       = $_FILES['image_name']['size'][$i];

            // File upload configuration
            $uploadPath = './assets/images/profiles/';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'jpg|jpeg|png|gif';

            // Load and initialize upload library
            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            // Upload file to server
            if($this->upload->do_upload('file')){
                // Uploaded file data
                $imageData = $this->upload->data();
                 $uploadImgData[$i]['image_name'] = $imageData['file_name'];

            }
        }
         if(!empty($uploadImgData)){
            // Insert files data into the database
            $this->pages_model->multiple_images($uploadImgData);              
        }
  }
  
Model
---------
  public function multiple_images($image = array()){

     return $this->db->insert_batch('profile_images',$image);
             }

Comment

upload multiple image using jquery ajax in codeigniter

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function (e) {
                $('#upload').on('click', function () {
                    var form_data = new FormData();
                    var ins = document.getElementById('multiFiles').files.length;
                    for (var x = 0; x < ins; x++) {
                        form_data.append("files[]", document.getElementById('multiFiles').files[x]);
                    }
                    $.ajax({
                        url: 'ajaxmultiplefileupload/upload_files', // point to server-side controller method
                        dataType: 'text', // what to expect back from the server
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: form_data,
                        type: 'post',
                        success: function (response) {
                            $('#msg').html(response); // display success response from the server
                        },
                        error: function (response) {
                            $('#msg').html(response); // display error response from the server
                        }
                    });
                });
            });
</script>
Comment

PREVIOUS NEXT
Code Example
Php :: symfony connect rabbitMQ 
Php :: laravel with and where 
Php :: php class file upload 
Php :: php version command linux 
Php :: carbon add and subtract 
Php :: update query laravel 
Php :: get the number of affected rows in php using pdo update statement 
Php :: how simple get ip address json 
Php :: livewire from one component to another 
Php :: script inside php 
Php :: php while jump to next loop 
Php :: group_concat mysql limit issue 
Php :: eloquent first 
Php :: html in php function 
Php :: php hour between 
Php :: get return value from another function laravel 
Php :: toastr in php 
Php :: php query to hide duplicate records 
Php :: get number of days between two dates php 
Php :: smarty php 
Php :: yii1 refresh cache schema 
Php :: php try to decode json 
Php :: Creating (Declaring) PHP Variables 
Php :: php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider 
Php :: php join 
Php :: laravel belongstomany prevent duplicates attach 
Php :: in array php 
Php :: types of method in api 
Php :: laravel merge two arrays helper 
Php :: match php 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =