Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==============================================================
- config for upload (../config/config.php)
- ==============================================================
- <?php
- $config['upload_path'] = 'upload/';
- $config['upload_tipe'] = 'gif|jpg|png';
- $config['upload_max_size'] = '1000';
- ==============================================================
- function for upload
- ==============================================================
- <?php
- public function upload_gambar()
- {
- $config['upload_path'] = $this->config->item('upload_path');
- $config['allowed_types'] = $this->config->item('upload_tipe');
- $config['max_size'] = $this->config->item('upload_max_size');
- $this->load->library('upload',$config);
- if (!$this->upload->do_upload()) {
- echo $this->upload->display_errors();
- } else {
- $file_data = $this->upload->data();
- $file_path = $this->config->item('upload_path').$file_data['file_name'];
- echo "Sukses upload file: ".$file_data['file_name']."<br />Lokasi file di: ".$file_path;
- }
- }
- ==============================================================
- html & script for upload picture with ajax processing
- ==============================================================
- <?php
- $attributes = array('class' => 'form-upload','enctype' => 'multipart/form-data', 'id' => 'myFRM');
- echo form_open('<<URL_UPLOAD_HERE>>',$attributes);
- ?>
- <div id="pesan"></div>
- <span id="loading"></span>
- <h2 class="form-signin-heading">Upload gambar</h2>
- <div class="form-group">
- <label for="fileinput">File input</label>
- <input type="file" id="fileinput" name="userfile" class="form-control" >
- </div>
- <button class="btn btn-lg btn-primary btn-block" type="submit" id="btn_upload" class="btn btn-default">Upload</button>
- <script>
- $(document).ready(function() {
- $("#myFRM").on('submit',(function(e) {
- e.preventDefault();
- $.ajax({
- url: "<<URL_UPLOAD_HERE>>",
- type: "POST",
- data: new FormData(this),
- mimeType:"multipart/form-data",
- contentType: false,
- cache: false,
- processData:false,
- beforeSend:function()
- {
- $("#pesan").hide();
- $("#loading").html('Processing upload file...Please wait...');
- },
- complete:function()
- {
- $("#loading").empty();
- $("#pesan").show();
- },
- error: function()
- {
- $('#pesan').html('Error, unknown');
- },
- success: function(data)
- {
- $("#pesan").html(data);
- }
- });
- }));
- });
- </script>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement