Advertisement
virbo

Upload File With CI and Ajax

Aug 1st, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. ==============================================================
  2.  config for upload (../config/config.php)
  3. ==============================================================
  4. <?php
  5.     $config['upload_path'] = 'upload/';
  6.     $config['upload_tipe'] = 'gif|jpg|png';
  7.     $config['upload_max_size'] = '1000';
  8.  
  9.  
  10. ==============================================================
  11.  function for upload
  12. ==============================================================
  13. <?php
  14. public function upload_gambar()
  15. {
  16.     $config['upload_path'] = $this->config->item('upload_path');
  17.     $config['allowed_types'] = $this->config->item('upload_tipe');
  18.     $config['max_size'] = $this->config->item('upload_max_size');
  19.     $this->load->library('upload',$config);
  20.  
  21.     if (!$this->upload->do_upload()) {
  22.        echo $this->upload->display_errors();
  23.     } else {    
  24.        $file_data = $this->upload->data();
  25.        $file_path = $this->config->item('upload_path').$file_data['file_name'];
  26.        echo "Sukses upload file: ".$file_data['file_name']."<br />Lokasi file di: ".$file_path;
  27.     }
  28. }
  29.  
  30.  
  31. ==============================================================
  32.  html & script for upload picture with ajax processing
  33. ==============================================================
  34. <?php
  35.      $attributes = array('class' => 'form-upload','enctype' => 'multipart/form-data', 'id' => 'myFRM');
  36.      echo form_open('<<URL_UPLOAD_HERE>>',$attributes);
  37. ?>
  38. <div id="pesan"></div>
  39. <span id="loading"></span>
  40. <h2 class="form-signin-heading">Upload gambar</h2>
  41. <div class="form-group">
  42.     <label for="fileinput">File input</label>
  43.     <input type="file" id="fileinput" name="userfile" class="form-control" >
  44. </div>
  45. <button class="btn btn-lg btn-primary btn-block" type="submit" id="btn_upload" class="btn btn-default">Upload</button>
  46. <script>
  47.             $(document).ready(function() {
  48.                 $("#myFRM").on('submit',(function(e) {
  49.                     e.preventDefault();
  50.                     $.ajax({
  51.                         url: "<<URL_UPLOAD_HERE>>",
  52.                         type: "POST",
  53.                         data: new FormData(this),
  54.                         mimeType:"multipart/form-data",
  55.                         contentType: false,
  56.                         cache: false,
  57.                         processData:false,
  58.                         beforeSend:function()
  59.                         {
  60.                             $("#pesan").hide();
  61.                             $("#loading").html('Processing upload file...Please wait...');
  62.                         },
  63.                         complete:function()
  64.                         {
  65.                             $("#loading").empty();
  66.                             $("#pesan").show();
  67.                         },
  68.                         error: function()
  69.                         {
  70.                             $('#pesan').html('Error, unknown');
  71.                         },
  72.                         success: function(data)
  73.                         {
  74.                             $("#pesan").html(data);
  75.                         }
  76.                     });
  77.                 }));
  78.             });
  79. </script>
  80. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement