简体   繁体   中英

Why is my Content-Length header wrong?

I am trying to find out the exact length of a string using strlen() in php 5.2. The string ($data) contains '\\t' and '\\n'.

echo strlen($data);

Code:

    // fetch table header
      $header = '';
      while ($fieldData = $result->fetch_field()) {
        $header .= $fieldData->name . "\t";
      }

      // fetch data each row, store on tabular row data
      while ($row = $result->fetch_assoc()) {
        $line = '';
        foreach($row as $value){
          if(!isset($value) || $value == ""){
            $value = "\t";
          }else{
            // important to escape any quotes to preserve them in the data.
            $value = str_replace('"', '""', $value);
            // needed to encapsulate data in quotes because some data might be multi line.
            // the good news is that numbers remain numbers in Excel even though quoted.
            $value = '"' . $value . '"' . "\t";
          }

          $line .= $value;
        }
        $data .= trim($line)."\n";
      }

      // this line is needed because returns embedded in the data have "\r"
      // and this looks like a "box character" in Excel
      $data = str_replace("\r", "", $data);

      // Nice to let someone know that the search came up empty.
      // Otherwise only the column name headers will be output to Excel.
      if ($data == "") {
        $data = "\nno matching records found\n";
      }

      // create table header showing to download a xls (excel) file
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=$export_filename");
      header("Cache-Control: public");
      header("Content-length: " . strlen($data); // tells file size
      header("Pragma: no-cache");
      header("Expires: 0");

  // output data
  echo $header."\n".$data;

This does not return the exact length (its less than the actual length). Please advice.

You are telling the user agent to expect strlen($data) and then actually sending $header."\\n".$data! Try something like this at the end of your code...

  $output=$header."\n".$data;

  // create table header showing to download a xls (excel) file
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=$export_filename");
  header("Cache-Control: public");
  header("Content-length: " . strlen($output); // tells file size
  header("Pragma: no-cache");
  header("Expires: 0");

  // output data
  echo $output;

strlen returns the correct answer:

echo strlen("1\n2\t3");

// prints 5

You will need to examine your input more carefully.

You echo both $header and $data , but you only set Content-Length to the size of $data .

If $header contains additional HTTP-headers, you should output $header using header() . Otherwise, you should set Content-Length to strlen($header."\\n".$data) .

在获取字符串长度之前,使用str_replace或其他函数从$data删除'\\t''\\n'符号,如果strlen()确实有bug(我没有检查过)。

The content-length header does NOT include the length of the headers plus the body of the response.

Content-Length: The length of the response body in octets (8-bit bytes)

Just FYI since the variables used in the "answer" are header + data. Do not be misled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM