php 为什么我的 Content-Length 标头错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3561596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:13:57  来源:igfitidea点击:

Why is my Content-Length header wrong?

phpexcelstringstrlen

提问by kobra

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'.

我试图在 php 5.2 中使用 strlen() 找出字符串的确切长度。字符串 ($data) 包含 '\t' 和 '\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.

这不会返回确切的长度(小于实际长度)。请指教。

回答by Paul Dixon

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...

你告诉用户代理期待 strlen($data) 然后实际发送 $header."\n".$data! 在代码的末尾尝试这样的事情......

  $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;

回答by mooyah

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

content-length 标头不包括标头的长度加上响应的正文。

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

Content-Length:以八位字节为单位的响应正文的长度(8 位字节)

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

仅供参考,因为“答案”中使用的变量是标题+数据。不要被误导。

回答by webbiedave

strlenreturns the correct answer:

strlen返回正确答案:

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

// prints 5

You will need to examine your input more carefully.

您将需要更仔细地检查您的输入。

回答by good_evening

Before getting length of string, delete '\t'and '\n'symbols from $datawith str_replaceor with other function like that if strlen()really has bug (I haven't checked that).

在获取字符串长度之前,如果确实有错误(我还没有检查过),请从with或 with 其他函数中删除'\t''\n'符号。$datastr_replacestrlen()

回答by Micke

You echo both $headerand $data, but you only set Content-Lengthto the size of $data.

您同时回显$header$data,但您只设置Content-Length了 的大小$data

If $headercontains additional HTTP-headers, you should output $headerusing header(). Otherwise, you should set Content-Lengthto strlen($header."\n".$data).

如果$header包含额外的 HTTP 标头,则应$header使用header(). 否则,您应该设置Content-Lengthstrlen($header."\n".$data).