0

I try to download a pdf in php but I can do it? Can anyone help me?
I try do it with this code but it doesn't works.

$file ="cv0.pdf"; 
$filename = "cv0descargado.pdf"; // el nombre con el que se descargara, puede ser diferente al original 
header("Content-type: application/octet-stream"); 
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=\"$filename\"\n"); readfile($file); 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0
$file = $_GET["file"];
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
    // header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;

Please try this way

Ronak
  • 36
  • 5
0

Use php with js:

<?php $path = "yourfilepath/name.pdf"; ?>

<button onclick="imageClick('<?php echo $path ?>')">Click</button>

<script>
    function imageClick(url) {
    var a = document.createElement("a");
    a.target = "_blank";
    a.href = url;
    a.click();
        }
</script> 
Community
  • 1
  • 1
Diogo Eira
  • 34
  • 8