php - download an existing file using codeigniter -
i'm trying make website can download jpg,png,pdf,docx file uploaded admin.
uploading content working without problem. when uploading insert file name in mysql table well. using table i'am showing uploaded file names users.
this code i'am using display files.
<?php if (!empty($downloads)) { foreach ($downloads $val) { ?> <div class="col-md-3 col-sm-6 col-xs-12"> <div class="panel panel-danger"> <div class="panel-heading panel-title"> <?php echo $val['upload_description']; ?> </div> <div class="panel-body"> <?php echo $val['file_name']; ?> <div class="clear-fix clear clearfix"></div> <div id="download" onclick="downloadfile('<?php echo $val['file_name']; ?>');" class="btn btn-danger">download</div> </div> </div> </div> <?php } } ?>
and ajax code
function downloadfile(str) { //alert(str); $.ajax({ type: 'post', url: 'downloadfiles', data: {value: str}, success: function(data) { alert('ok' + data); } }); }
when 1 click on download button, using ajax , jquery i'am sending file name download_controller. function in download_controller file
public function download_files() { $filename = $this->input->post('value'); $file_path = 'uploaded/' . $filename; $this->_push_file($file_path, $filename); } function _push_file($path, $name) { $this->load->helper('download'); // make sure it's file before doing anything! if (is_file($path)) { // required ie if (ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'off'); } // file mime type using file extension $this->load->helper('file'); $mime = get_mime_by_extension($path); // build headers push out file properly. header('pragma: public'); // required header('expires: 0'); // no cache header('cache-control: must-revalidate, post-check=0, pre-check=0'); header('last-modified: ' . gmdate('d, d m y h:i:s', filemtime($path)) . ' gmt'); header('cache-control: private', false); header('content-type: ' . $mime); // add mime type code igniter. header('content-disposition: attachment; filename="' . basename($name) . '"'); // add file name header('content-transfer-encoding: binary'); header('content-length: ' . filesize($path)); // provide file size header('connection: close'); readfile($path); // push out exit(); } }
but downloading never starting. when trying download png file i'm getting kind of message.
can 1 tell me wrong thing i'am doing here?
thank you
i found did wrong. mr.kranebird
i change ajax code this
$.ajax({ type: 'post', url: 'downloadfiles', data: {value: str}, success: function(data) { alert('ok' + data); } });
to this.
window.location.href = "<?php echo base_url().'downloadfiles/'?>" + str;
without posting download_controller redirect download_controller.
now working without error.
thank every 1 waste valuable time me.
Comments
Post a Comment