Not able to download files with php in android browsers -
when using computer files downloaded normally. problem start on android browsers.
when download file android default browser, file downloaded of 0.00 bytes, not technically present there(corrupt file).
when download file third party application or opera gives error "file not downloaded." not header error. , files downloaded via uc browser , chrome , firefox. still gives error in default browser.
here code using:
<?php require 'php/db.php'; if(isset($_post['file_id'])&&!empty($_post['file_id'])){ download_file($_post['file_id']); } function download_file($id){ global $con; $id = mysqli_real_escape_string($con,htmlentities($id)); $file="select file_name,file_title,file_size,down files file_id= $id"; $result = mysqli_query($con,$file); $row = mysqli_fetch_assoc($result); $name = $row['file_name']; $title = $row['file_title']; $size = $row['file_size']; $ext = strtoupper(ext($name)); // function defined bellow $down = $row['down']; $newname = $title.'.'.$ext; $olddir = "files/".$name; $down++; if(is_file($olddir)) { $update_down = "update files set down = $down file_id = '$id'"; $update_down_result = mysqli_query($con,$update_down); 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 ($olddir)).' gmt'); header('cache-control: private',false); header('content-type: application/octet-stream'); header('content-disposition: attachment; filename="'.$newname.'"'); header('content-transfer-encoding: binary'); header('content-length: '.$size); // provide file size header('connection: close'); readfile($olddir); exit(); }else header("location: index.php?msg=sorry!+file+could+not+be+downloaded"); } function ext($name){ $rut = strrev($name); $erut = explode('.', $rut); return strrev($erut[0]); }
we give file id function , downloads file on pc.
can tell me how remove error? users can download files android phones too.
probably $size == 0;
$size = $row['file_size']; ... $olddir = "files/".$name;
change to
$olddir = "files/".$name; $size = filesize($olddir);
and change
else header("location: index.php?msg=sorry!+file+could+not+be+downloaded");
to
else header("location: index.php?msg=sorry!+file+could+not+be+found+on+server: " . $olddir);
Comments
Post a Comment