Sathishkumar Sathishkumar - 2 months ago 18
PHP Question

Downloading err on php mysql

I've php code for downloding files from Mysql database , but while i click the link the following error will appear,

"Warning: Cannot modify header information - headers already sent by
(output started at /home/somelocation/loc/coks/getfiles.php:2)
" .

// Make sure an ID was passed
if(isset($_GET['id'])) {
// Get the ID
$id = intval($_GET['id']);

// Make sure the ID is in fact a valid ID
if($id <= 0) {
die('The ID is invalid!');
else {
// Connect to the database
$dbLink = new mysqli('localhost', 'username', '1235@e', 'Databases');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());

// Fetch the file information
$query = "
SELECT `mime`, `name`, `size`, `data`
FROM `resume_file`
WHERE `id` = {$id}";
$result = $dbLink->query($query);

if($result) {
// Make sure the result is valid
if($result->num_rows == 1) {
// Get the row
$row = mysqli_fetch_assoc($result);

// Print headers
header("Content-Type: ". $row['mime']);
header("Content-Length: ". $row['size']);
header("Content-Disposition: attachment; filename=". $row['name']);

// Print data
echo $row['data'];
else {
echo 'Error! No image exists with that ID.';

// Free the mysqli resources
else {
echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
else {
echo 'Error! No ID was passed.';

what is the problem in above code please, anyone help me.
note : please check line 32, 33,34.



We already have an answer for this.

Try this link.

How to fix "Headers already sent" error in PHP

This is due to the whitespaces in ur code. Check that url for more details

output started at /home/somelocation/loc/coks/getfiles.php:2

u have some whitespace before your <?php thats why the error shows like loc/coks/getfiles.php:2 your <?php starts from line no 2 only :)