http headers - Error: Downloading broken file by fread in php -
i tried different codes download files php.
fileread() give issues so, not option now.
i trying use fread() now, problem gives when download file, file never works. download broken file. example if download image file give error invalid image in picasa. (shown below)
following code using:
<?php require '../php/db.php'; // database connection (no issue in this) ob_start(); set_time_limit(0); // set script time infinity. ini_set('memory_limit', '512m'); if(isset($_get['file_id'])&&!empty($_get['file_id'])) download_file($_get['file_id']); // calling function download_file given below else die("there error in downloading file. please try again later."); function download_file($id){ global $con; // database connection $id = mysqli_real_escape_string($con,htmlentities($id)); $file="select file_name,file_title,file_size,down files file_id= $id"; // taking file info database $result = mysqli_query($con,$file); if($result) echo "okay!"; $row = mysqli_fetch_assoc($result); $name = $row['file_name']; // file stored name in same dir $title = $row['file_title']; $ext = ext($name); $down = $row['down']; // tells number of downloads $newname = $title.'.'.$ext; // changing file name while downloading. $down++; if(is_file($name)) { $update_down = "update files set down = $down file_id = '$id'"; $update_down_result = mysqli_query($con,$update_down); // increment number of downloads download_by_fread($name,$newname); // calling function exit; }else header("location: ../index.php?msg=sorry!+file+could+not+found!"); } function download_by_fread($name,$newname){ if($fd=fopen($name, "rb")){ header("pragma: public"); header("expires: 0"); header("cache-control: must-revalidate, post-check=0, pre-check=0"); header("cache-control: private",false); header("content-type: ".tell_file_type($name)); header("content-disposition: attachment; filename=\"".$newname."\""); header("content-length: ".filesize($name)); header("cache-control: public"); //use open files directly while(!feof($fd)) { print(fread($fd, 4096)); ob_flush(); flush(); } }else header("location: ../index.php?msg=sorry!+file+could+not+found!"); fclose($fd); } function ext($name){ $rut = strrev($name); $erut = explode('.', $rut); return strrev($erut[0]); } function tell_file_type($file_name){ $rut = strrev($file_name); $erut = explode('.', $rut); $ext = strrev($erut[0]); switch($ext){ case 'txt': return 'text/plain'; break; case 'rar': case 'zip': return 'application/x-compressed'; break; case 'gif': return 'image/gif'; break; case 'jpg': case 'jpeg': return 'image/jpeg'; break; case 'bmp': return 'image/bmp'; break; case 'png': return 'image/png'; break; case 'pdf': return 'application/pdf'; break; case 'mp3': return 'audio/mpeg3'; break; case 'mp4': return 'video/mp4'; break; case 'mkv': return 'video/mkv'; break; case 'mpg': return 'video/mpeg'; break; case 'avi': return 'video/avi'; break; case 'wav': return 'audio/wav'; break; case 'doc': case 'docx': return 'application/msword'; break; case 'pps': case 'ppt': case 'pptx': return 'application/mspowerpoint'; break; case 'xls': case 'xlsx': return 'application/excel'; break; case 'exe': return 'application/octet-stream'; break; case 'swf': return 'application/x-shockwave-flash'; break; } } ?>
help me finding error in code please.
thank you.
answer: found error. echoing "okay!" after mysql_query. check if query successful. can or added file. , file become invalid.
remove ?>
ends of php files, , make there no spaces before <?php
@ beginning.
what may happening php sending " " blank space characters output of image. cause corrupt file if there outside of php brackets these automatically sent browser content.
Comments
Post a Comment