Problem method GET php when reading a file txt xxx.php? txt = file.txt?

0

Hi, I'm asking this question since I'm a bit stuck with GEt php, I know I want to read, not download a txt, either with url externna, I'm new to php, I still struggle a little to understand. I want to create urls like this xxx.php? Link = link method get or so xxx.php? Txt = file.txt What happens that this code only downloads does not read it; "I want you to read me the file that says", I want to know what I'm wrong with :( Thanks my code is

<?php

if (!isset($_GET['file']) || empty($_GET['txt'])) {
  exit();
}
$root = "txt/";
$file = basename($_GET['file']);
$path = $root.$file;
$type = '';

if (is_file($path)) {
  $size = filesize($path);

  if (function_exists('mime_content_type')) {
    $type = mime_content_type($path);
  }
  else if (function_exists('finfo_file')) {
    $info = finfo_open(FILEINFO_MIME);
    $type = finfo_file($info, $path);
    finfo_close($info);
  }

  if ($type == '') {
    $type = "application/force-download";
  }

  // Definir headers
  header("Content-Type: $type");
  header("Content-Disposition: attachment; filename=$file");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: " . $size);

  // Descargar archivo
  readfile($path);
}
else {
  die("El archivo txt no existe.");
}

?>
    
asked by Víctor M 25.10.2018 в 23:34
source

1 answer

2

Good morning, to read a file from php you use the function fgets ()

<?php
$gestor = @fopen("/tmp/inputfile.txt", "r");
if ($gestor) {
    while (($búfer = fgets($gestor, 4096)) !== false) {
        echo $búfer;
    }
    if (!feof($gestor)) {
        echo "Error: fallo inesperado de fgets()\n";
    }
    fclose($gestor);
}
?>
    
answered by 25.10.2018 / 23:46
source