0

I am working on a project, where I am displaying files from a directory. When the user clicks on one of the row, then it calls a different PHP file, where a PDF file is downloaded, and other code is shown. Currently, when I click on the link, only the file is getting downloaded, and I cannot see the PHP page. What am I doing wrong?

use-case : The PDF downloaded is to be filled by the user and then uploaded again.. The code for upload and further processing is on send-mail.php. So, I need to download the file and send the user to send-mail.php as well. That's the use-case.

Original PHP from where redirect happens :

<?php
    echo "<table  align='center' class='loopblock'>";
    echo "<tr>
    <th>Existing templates</th>
    </tr>";    
    $path = "/var/www/html/pdf/";
    $files = scandir($path);
    $files = array_diff(scandir($path), array('.', '..'));
    foreach($files as $key){
    echo"<tr class='loop'>
        <td class='click' align='center' width='500' class='loop'><a class='loop' align='center'
          href='/send-email.php?fileName=$key'>$key</a></td>                        
        </a>    
        </tr>";     
    }       
    echo"</table>";
?>

send-email.php : When the PHP code below is present, I don't see the HTML code.

<div class="container">
        <div class="contr"><h2>Drag and Drop your Tempalates to 'Drop Area' (size - under 10Mb)</h2></div>
        <div class="upload_form_cont">
            <div id="dropArea">Drop Area</div>
            <div class="info">
                <div>Files left: <span id="count">0</span></div>
                <div>Destination url: <input id="url" value="/upload.php" readonly/></div>
                <h2>Result:</h2>
                <div id="result"></div>
            <canvas width="500" height="20"></canvas>
            </div>
        </div>
</div>
<script src="js/script.js"></script>
<?php
    ignore_user_abort(true);
    set_time_limit(0); // disable the time limit for this script

    $path = "/var/www/html/pdf/"; // change the path to fit your websites document structure

    $dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['fileName']); // simple file name validation
    $dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
    $fullPath = $path.$dl_file;

    if ($fd = fopen ($fullPath, "r")) {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
        switch ($ext) {
            case "pdf":
            header("Content-type: application/pdf");
            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
            break;
            // add more headers for other content types here
            default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
            break;
        }
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }
    fclose ($fd);
?> 

Any ideas what I might be doing wrong? Thank you.

Update

Changed it to something like this :

<div class="container">
        <div class="contr"><h2>Drag and Drop your Tempalates to 'Drop Area' (size - under 10Mb)</h2></div>
        <div class="upload_form_cont">
            <div id="dropArea">Drop Area</div>
            <div class="info">
                <div>Files left: <span id="count">0</span></div>
                <div>Destination url: <input id="url" value="/upload.php" readonly/></div>
                <h2>Result:</h2>
                <div id="result"></div>
            <canvas width="500" height="20"></canvas>
            </div>
        </div>
</div>
<script src="js/script.js"></script>
<?php
session_start();
    ignore_user_abort(true);
    set_time_limit(0); // disable the time limit for this script

    $path = "/var/www/html/pdf/"; // change the path to fit your websites document structure

    $dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['fileName']); // simple file name validation
    $dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
    $fullPath = $path.$dl_file;

    if ($fd = fopen ($fullPath, "r")) {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
        switch ($ext) {
            case "pdf":
            header("Content-type: application/pdf");
            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
            break;
            // add more headers for other content types here
            default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
            break;
        }
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
        fclose ($fd);
        exit();
    }
?> 
</body>
</html>
We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • You've sent output **before** setting the header. This won't work because the header was already set. See [this](https://stackoverflow.com/a/54653212/8398549) answer I posted yesterday. – Cid Feb 14 '19 at 13:31
  • @Cid : I read your answer, and added a session.start as the php code started and exit inside the if loop, still nothing. I have also updated the main post(bottom part) to include the change. Thank you. – We are Borg Feb 14 '19 at 13:36
  • An HTTP request can only send one type of response. Either it sends a file to be downloaded, or it sends a page to be displayed. It can't do both at the same time. – Barmar Feb 14 '19 at 13:40
  • @Barmar : Can I somehow include the download code in another PHP and then trigger it. Many sites redirect after download. I want to do the same thing. – We are Borg Feb 14 '19 at 13:41
  • You could include JavaScript in the returned page that uses `window.open()` to open a second window that performs the download. – Barmar Feb 14 '19 at 13:42
  • @Barmar : Not familiar with JS, can you tell me how I can do that? Thank you. – We are Borg Feb 14 '19 at 13:43
  • ``. Note that this might be blocked by the browser's popup blocker. – Barmar Feb 14 '19 at 13:44
  • @Barmar : And after that, how can I redirect to the PHP page as well? Thanks. – We are Borg Feb 14 '19 at 13:47
  • You don't have to redirect. This is part of the PHP page. – Barmar Feb 14 '19 at 13:47
  • @Barmar : No, that's not what I mean. Shall we discuss this a bit. Thanks. – We are Borg Feb 14 '19 at 13:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188423/discussion-between-we-are-borg-and-barmar). – We are Borg Feb 14 '19 at 13:49
  • @Barmar : The PDF downloaded is to be filled by the user and then uploaded again.. The code for upload and further processing is on send-mail.php. So, I need to download the file and send the user to send-mail.php as well. That's the use-case. – We are Borg Feb 14 '19 at 13:52

1 Answers1

0

I finally ended up adding this to solve the problem :

<?php
$invite = $_GET['fileName'];
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=download_pdf.php?fileName=' . $invite . '">'; 
?>
We are Borg
  • 5,117
  • 17
  • 102
  • 225