php header <!-- Core Error --> and file currupted

95 Views Asked by At

I'm having a issue since I update my PHP to version 5.4.41.

I'm running on a nginx server with php 5.4.41.

I've made a php code that allow me to retrieve a file and download it. But since I updated my php code all my file that i'm trying to download get corrupted. So I upload a txt file and to see what was happening.

EX: If I upload a text file with test inside after I download it, it will become

<!--core error-->test

Here the complete code

<?php

require_once("include.php");
if(!xml2php("customer")) {
$smarty->assign('error_msg',"Error in language file");
}

//Grab the key from the url
$key = $VAR['key'];

if ($VAR['escape'] == 1) {

if (isset($key)) {

$q = "SELECT * FROM ".PRFX."TABLE_CUSTOMER_BACKUP WHERE     CUSTOMER_BACKUP_KEY=".$db->qstr($key);

if(!$rs = $db->Execute($q)) {
    force_page('core', 'error&error_msg=MySQL Error: '.$db-    >ErrorMsg().'&menu=1&type=database');
    exit;
    } else {

    $customer_backup_filename = $rs->fields['CUSTOMER_BACKUP_FILENAME'];
    $customer_backup_ext = $rs->fields['CUSTOMER_BACKUP_EXT'];
    $customer_backup_file_link = $rs->fields['CUSTOMER_BACKUP_FILE_LINK'];
    $customer_backup_filetype = $rs->fields['CUSTOMER_BACKUP_FILETYPE'];

    }
        if(empty($customer_backup_file_link)){

        force_page('core', 'error&error_msg=This key is not valid');
        exit;
        }

} else {
    force_page('core', 'error&error_msg=This key is not valid');
    exit;
    }

$fakeFileName= $customer_backup_filename.".".$customer_backup_ext;

$file = $customer_backup_file_link;



if (isset($customer_backup_file_link, $customer_backup_ext,      $customer_backup_filename, $customer_backup_filetype) &&      file_exists($customer_backup_file_link)){
     ignore_user_abort(true);
    set_time_limit(0); // disable the time limit for this script

    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Type: ".$customer_backup_filetype);
    header("Content-Disposition: attachment; filename=".$fakeFileName);
    header("Content-Length: ".filesize($file));
    header("Content-Transfer-Encoding: binary");

    readfile($file);

} else {
    force_page('core', 'error&error_msg=There was a problem downloading your  file. Please contact your administrator.');
    exit;
}

} else { 
force_page('core', 'error&error_msg=There was a problem downloading your   file. Please contact your administrator.');
exit;
}

$smarty->display('customer'.SEP.'download.tpl');

//exit;

?>

I discover if I put // in front of

header("Content-Type: ".$customer_backup_filetype);
header("Content-Disposition: attachment; filename=".$fakeFileName);

to ignore it the file will display into the browser without issue

I need some help I've been working on this issue for 2 days and haven't found nothing.

Update :

I discover if I try to escape the smarty template engine it will not work same issue. But if I load the smarty template engine the text will display without error.

http://www.exemple.com/index.php?page=customer:download&key=1414830421&escape=1 //Escaping the smarty template engine

2

There are 2 best solutions below

0
On BEST ANSWER

I forget to put in the code

    ob_clean();
    flush();
0
On

The <!--core error--> has to originate to some place before:

readfile($file);

Since this is the line where your content 'test' is written, which displays after it.

My guesses would be, that the source is either one of:

  • include.php
  • xml2php()
  • $db->Execute()

Maybe you could add additional output to your code, to narrow it down to the line, where <!--core error--> gets written.