trying to figure out a simple task. It is necessary when addressing the url to sort the data, having received a line at the output, write to a file and give it for downloading. The array is formed from the request to the database, then it is moved. The result is such a structure

foreach ($allRes as $row){ foreach ($row as $k => $v){ if($k != 'section_id') { $str.= $v . $col_delimiter. ' - ' . $count; } } $count++; $str .= $row_delimiter; } 

The result is a line with hyphenation.

I did it, everything works, but not all lines get into the downloaded file.

  header('Content-Disposition: attachment; filename="str.txt"'); header('Content-Type: text/plain'); header('Content-Length: ' . strlen($str)); header('Connection: close'); echo $str; 

Can anyone come across?

  • What is $str , how is this value formed? Also, when working with data in an encoding such as utf-8, strlen will give the wrong value. - u_mulder 2:46 pm
  • While foreach ($allRes as $row){ foreach ($row as $k => $v){ if($k != 'section_id') { $resSrt .= $v . $col_delimiter. ' - ' . $count; } } $count++; $str.= $row_delimiter; } over an array in str, I write the foreach ($allRes as $row){ foreach ($row as $k => $v){ if($k != 'section_id') { $resSrt .= $v . $col_delimiter. ' - ' . $count; } } $count++; $str.= $row_delimiter; } lines foreach ($allRes as $row){ foreach ($row as $k => $v){ if($k != 'section_id') { $resSrt .= $v . $col_delimiter. ' - ' . $count; } } $count++; $str.= $row_delimiter; } foreach ($allRes as $row){ foreach ($row as $k => $v){ if($k != 'section_id') { $resSrt .= $v . $col_delimiter. ' - ' . $count; } } $count++; $str.= $row_delimiter; } foreach ($allRes as $row){ foreach ($row as $k => $v){ if($k != 'section_id') { $resSrt .= $v . $col_delimiter. ' - ' . $count; } } $count++; $str.= $row_delimiter; } mb_internal_encoding("UTF-8"); header('Content-Disposition: attachment; filename="ads.txt"'); header('Content-Type: text/plain'); header('Content-Length: ' . strlen($str)); header('Connection: close'); echo $str; Code without receiving data. - Alex
  • Thanks, really not correctly length of a line counted. Although tried and mb_strlen added only 100, but still not all. - Alex

1 answer 1

You can not specify the Content-Length if you specify the Content-Transfer-Encoding header

A sender must not send a Content-Length header field. RFC

 header('Content-Type: text/plain'; header('Content-Disposition: inline;filename="str.txt"'); header('Cache-Control: max-age=0'); header('Content-Transfer-Encoding: binary'); // If you're serving to IE, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: ' . (new \DateTime('now'))->format('r')); header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 echo $str;