Problem when opening txt hosted on server

1

The fact is that I have a website where I have a link where when I press it, a txt that has several lines is downloaded, for example, it is like this:

  

123455

     

456789

     

234566

     

234354

But when you download it and open the file on my pc, the file appears like this:

  

123455456789234566234354

No line breaks, and I have no idea why this may be happening since the file is correctly uploaded on the server (with its line breaks). I have tested it in all the browsers and in all of them the file is downloaded without line breaks.

This is the php code that is responsible for downloading the file:

$fichero = $_GET['nombre'];
$basefichero = basename($_GET['nombre']);

header( 'Content-Type: application/octet-stream');

header( 'Content-Length: '.filesize($fichero));

header( 'Content-Disposition:attachment;filename=' .$basefichero);
readfile($fichero);'

What could be happening there?

Greetings.

    
asked by M. Giner 20.08.2018 в 12:47
source

1 answer

3

You're seeing it on windows, which uses line breaks of type " \r\n "also called CR (carriage return - carriage return) and LF (line feed - line feed).

Windows takes this form for compatibility with MS-DOS which inherits it from CP/M-80 , and it was a way to standardize output per screen and output per printer, which at that time were more wave electric typewriter or teletypes .

For those who ask themselves: what is a "car", and why should it "return"? watch the video of a teletype working in manual mode link

Unix / linux have implemented the "end of line" with a single character LF and let the device (if it has a car, is responsible for returning the car to the initial position of the line).

In the case of Microsoft Notepad - Notepad just supports showing line breaks well ( CR , CR + LF , LF ) from May 2018 in the insider version

In summary the file has line breaks unix LF and your version of the notebook does not show them because it lacks the CR .

To replace the line breaks when sending the file there are several ways, including 2:

1) with file ($ file) we read the file to an array of lines , we ignore the original line breaks and rebuild the file with "\r\n" .

<?php

$fichero = $_GET['nombre'];
$basefichero = basename($_GET['nombre']);

// por seguridad tendrías que limitar a una sola carpeta
// para evitar por ej: getfile.php?nombre=/etc/passwd
$carpetaTextos = __DIR__ . '/textos/';
$fichero = $carpetaTextos . $basefichero;

if (file_exists($fichero)) {
  header( 'Content-Type: text/plain');
  header( 'Content-Disposition:attachment;filename=' .$basefichero);

  // lee el archivo a un array de lineas
  $contenido = file($fichero, FILE_IGNORE_NEW_LINES);
  $contenido = implode("\r\n", $contenido);

  echo $contenido;
}
exit();
?>

2) with file_get_contents ($ file) we read the file and replace "\n" with "\r\n" .

<?php

$fichero = $_GET['nombre'];
$basefichero = basename($_GET['nombre']);

// por seguridad tendrías que limitar a una sola carpeta
// para evitar por ej: getfile.php?nombre=/etc/passwd
$carpetaTextos = __DIR__ . '/textos/';
$fichero = $carpetaTextos . $basefichero;

if (file_exists($fichero)) {
  header( 'Content-Type: text/plain');
  header( 'Content-Disposition:attachment;filename=' .$basefichero);

  // leer el archivo y reemplazar
  $contenido = file_get_contents($fichero);
  $contenido = str_replace("\n","\r\n",$contenido);

  echo $contenido;
}
exit();
?>
    
answered by 20.08.2018 / 17:04
source