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>

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 <form action='D:/wwwroot/Workspace/George/Goliath/modules/clientsupport/landing-myproco-proco2.php' method='post' enctype="multipart/form-data"> <p></p> <input type='submit' name='download' class="button yellow" value="Download"/> </form> but still nothing happens when the button is clicked. Could I still be missing something?
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
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
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.

2 Comments

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?
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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.