Failed - Server Problem while Downloading File from URL in PHP

256 views Asked by At

This has been really annoying me. When I try to download a file, it says Failed - Server problem. But Its actually working well, when I declare the file path directly

 $file = "wp-content/Devis_10.pdf";

But However I am trying to change the file path depending on the user sales id, is this case it shows download error.

$part2 = $current_user->sales_id; 
$part1 = "wp-content/Devis_";
$part3 = ".pdf";

What am I missing here ??

This is my complete code

<?php 
    
    global $current_user;
    get_currentuserinfo();
    $email = $current_user->user_email;
    $part2 = $current_user->sales_id;
    
    $part1 = "wp-content/Devis_";
    $part3 = ".pdf";
    
    
    header("Content-Type: application/octet-stream");
    $file = $part1.$part2.$part3;   //Result example "wp-content/Devis_10.pdf";
    header('Content-Disposition: attachment; filename="Devis.pdf"');  
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");            
    header("Content-Length: " . filesize($file));
    flush(); 
    $fp = fopen($file, "r");
    while (!feof($fp))
    {
        echo fread($fp, 65536);
        flush(); 
    }
    fclose($fp);
    
    
    ?>

HTML

<a href="https://www.xxxxxxxx.com/devis.php" download="Devis.pdf">
         <button type="button">Download</button>
         </a>
0

There are 0 answers