我的 Content-Length 标头错误?发生了什么?

发布于 2024-09-16 11:17:38 字数 1725 浏览 6 评论 0原文

我试图在 php 5.2 中使用 strlen() 找出字符串的确切长度。字符串 ($data) 包含“\t”和“\n”。

echo strlen($data);

代码:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分分钟 2024-09-23 11:17:38

您告诉用户代理期望 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;

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;
咆哮 2024-09-23 11:17:38

strlen 返回正确的答案:

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

// prints 5

您需要更多地检查您的输入小心。

strlen returns the correct answer:

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

// prints 5

You will need to examine your input more carefully.

风情万种。 2024-09-23 11:17:38

内容长度标头不包括标头长度加上响应正文。

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

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

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.

魂归处 2024-09-23 11:17:38

在获取字符串长度之前,请使用 str_replace$data 中删除 '\t''\n' 符号或者使用其他类似的函数,如果 strlen() 确实有错误(我还没有检查过)。

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

一抹淡然 2024-09-23 11:17:38

您回显 $header$data,但仅将 Content-Length 设置为 $data 的大小。

如果 $header 包含其他 HTTP 标头,则应使用 header() 输出 $header。否则,您应该将 Content-Length 设置为 strlen($header."\n".$data)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文