Integrating Uploadify with Codeigniter.

Hi Team,

This tutorial will help you to integrate uploadify with codeigniter.

Controller :-

public function do_upload()
{
$view_data = array();
$upload_config;
$this->load->helper(‘file’);
$this->load->library(‘upload’);
$this->load->library(‘session’);
$image_upload_folder = ‘./uploads/bulk’;

$user_id =  $this->uri->segment(3);
$listing_id =  $this->uri->segment(4);

if (!file_exists($image_upload_folder)) {
mkdir($image_upload_folder, DIR_WRITE_MODE, true);
}

$this->upload_config = array(
‘upload_path’   => $image_upload_folder,
‘allowed_types’ => ‘png|jpg|jpeg|bmp|tiff’,
‘max_size’      => 2048,
‘remove_space’  => TRUE,
‘encrypt_name’  => TRUE,
);

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

if (!$this->upload->do_upload()) {
$upload_error = $this->upload->display_errors();
// echo json_encode($upload_error);
write_file(‘./uploads/bulk/file.php’, “Inside If”);
echo “Inside IF”;
} else {
$file_info = $this->upload->data();
// echo json_encode($file_info);
$data[‘user_id’] = $user_id; // $this->session->userdata(‘userId’);
$data[‘filename’] = $file_info[‘file_name’];
$data[‘is_active’] = 0;
$data[‘listing_id’] = $listing_id; // $this->session->userdata(‘listingId’);
//write_file(‘./uploads/bulk/file.php’, $file_info[‘file_name’]);
$flag = $this->listings->addImages($data);
}

}

HTML

<link href=”<?php echo base_url(); ?>/assets/css/uploadify.css” type=”text/css” media=”screen” rel=”stylesheet”/>

<script>window.jQuery || document.write(‘<script src=”<?php echo base_url(); ?>assets/js/jquery-min.js”><\/script>’)</script>
<script src=”<?php echo base_url(); ?>assets/js/jquery.uploadify.min.js” type=”text/javascript”></script>

<script type=”text/javascript”>
$(document).ready(function () {

var base_url = ‘<?php echo base_url(); ?>’;

$(‘#upload-file’).click(function (e) {
e.preventDefault();
$(‘#userfile’).uploadify(‘upload’, ‘*’);
});

$(‘#userfile’).uploadify({

‘auto’:false,
‘swf’: base_url + ‘assets/js/uploadify.swf’,
‘uploader’: base_url + ‘index.php/Listing/do_upload/<?php echo $this->session->userdata(‘userId’); ?>/<?php echo $this->session->userdata(‘listingId’); ?>’,
‘cancelImg’: base_url + ‘assets/img/uploadify-cancel.png’,
‘fileTypeExts’:’*.jpg;*.bmp;*.png;*.tif’,
‘fileTypeDesc’:’Image Files (.jpg,.bmp,.png,.tif)’,
‘fileSizeLimit’:’2MB’,
‘fileObjName’:’userfile’,
‘buttonText’:’Select Photo(s)’,
‘multi’:true,
‘removeCompleted’:false
});
});
</script>

<?php echo form_open_multipart(); ?>
<ul class=”unstyled”>
<li>
<?php echo form_upload(‘userfile’,”,’id=”userfile”‘); ?>
<?php echo (isset($error)) ? $error : ”; ?>
</li>
<li>
<?php echo form_button(array(‘content’=> ‘Upload’, ‘id’=>’upload-file’, ‘class’=>’btn btn-large btn-primary’)); ?>
</li>
</ul>
<?php echo form_close(); ?>

Filed Under: Uncategorized

About the Author:

RSSComments (0)

Trackback URL

Leave a Reply