1

I have a form with an input submit button, and want it so that when the button is clicked, it downloads a file.

My if statement basically just checks the file exists, and if so starts to download the file. That part works, however I can't get it to work so that it downloads the file ONLY when the button is clicked.

At the moment, nothing happens when the button is clicked.

Maybe I'm not putting the if statement in the correct place?

      <form action='?module=clientsupport&call=landing-proco' method='post' enctype="multipart/form-data">           
        <input type='submit' name='download' class="button yellow" value="Download"/>

        <?php
        if (file_exists("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt")) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt") . '"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt"));
            readfile("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt");
            sleep(2);
            unlink("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt");
            exit;
        }
        ?>

    </form>
ginomay89
  • 249
  • 3
  • 17

2 Answers2

1

Remove php code from html, create new file with your PHP code and link action of your form to this file. It will work fine

Nevermind23
  • 375
  • 1
  • 4
  • 14
  • I have tried this and created a new php file only containing my if statement, and then in the other file the action is now the path and name of this new php file. My code now reads `

    ` but still nothing happens when the button is clicked. Could I still be missing something?
    – ginomay89 Mar 01 '19 at 11:21
  • Are you using apache or something else to run webserver ? By default PHP code is not executing, if you access `D:/wwwroot/Workspace/George/Goliath/modules/clientsupport/landing-myproco-proco2.php` path, it will just download your php file, so route must be something like `http://localhost/modules/clientsupport/landing-myproco-proco2.php` – Nevermind23 Mar 01 '19 at 11:26
  • this pointed me in the right direction regarding creating a separate php file for the if statement. with some tweaking i managed to get this to work so thank you – ginomay89 Mar 01 '19 at 16:08
0

Look at your program.

Line 1:

     <form action='?module=clientsupport&call=landing-proco' method='post' enctype="multipart/form-data">           

… starts outputting an HTML document.

Then you get to:

 header('Content-Description: File Transfer');

where you start to output something that isn't an HTML document, but it is too late because you are already in the middle of an HTML document.

Design your logic to output one thing or the other.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • sorry I'm slightly confused by this. Are you saying I can't use the PHP header() function because I have already used HTML above it? – ginomay89 Mar 01 '19 at 10:29
  • 2
    Yes. You have to output the headers before you output anything else. They go at the top. That's why they are called headers. HTML is "anything else". – Quentin Mar 01 '19 at 10:39