Problems downloading pdf by Chrome PHP

2

At the moment of selecting a pdf, the chrome browser visualizes it well, but if I want to download it, it does not work for me, it downloads a file with the name of the page that is ver.php

index.php

var listar = function(year) {
            var table = $("#dt_cliente").DataTable({
                "order": [
                    [0, "desc"]
                ],
                "destroy": true,
                "ajax": {
                    "method": "POST",
                    "url": "inc/listar.php?anno=" + year //Año
                },
                "columns": [{
                    "data": "decreto"
                }, {
                    "data": "anno"
                }, {
                    "data": "materia",
                "searchable": true,
                "sortable": false
                }, {
                    "data": "pdf",
                    "searchable": false,
                    "sortable": false,
                    "render": function(pdf) {
                        if (!pdf) {
                            return "";
                        } else {
                            return '<a href="ver.php?pdf=' + pdf + '" target="_blank"><img src="img/pdf-icon.png"> Ver decreto</a>'
                        }
                    }
                }]

ver.php

<?php
header('Content-Description: File Transfer');
header("Content-type: application/pdf");
readfile('file/'.$_GET['pdf']);
 ?>

If I use mozilla it works very well, what can it be? Best regards

link here I try to download the pdf in Chrome

    
asked by MoteCL 30.04.2018 в 20:23
source

1 answer

1

1 with get: ver.php?pdf=nombre.pdf

Defining inline so that it leaves by browser and filename so that it takes the original name

<?php
$nombreDeArchivo = $_GET['pdf'];
$nombreDeArchivo = end(explode('/',$nombreDeArchivo));

header('Content-Description: File Transfer');
header("Content-type: application/pdf");
header('Content-Disposition: inline; filename='.$nombreDeArchivo);
readfile('file/'.$nombreDeArchivo);
?>

2 using path info: ver.php/nombre.pdf

<?php
$nombreDeArchivo = ltrim(isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'ERROR','/');
$nombreDeArchivo = end(explode('/',$nombreDeArchivo));

header('Content-Description: File Transfer');
header("Content-type: application/pdf");
header('Content-Disposition: inline; filename='.$nombreDeArchivo);
readfile('file/'.$nombreDeArchivo);
 ?>

In the examples filter minimally the file name, in your case if they are only pdf you should check the extension, if the file exists and those things so that there are no security holes or strange errors.

    
answered by 01.05.2018 / 00:13
source