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();
?>