Build Your Own Database Driven Web Site Using PHP & MySQL

#1

Hello, i am just getting start with MySQl and PHP, i flowed the course it was so helpful.
Now i am trying to do the same thing here but with “Download” button in order to download the joke in .txt file . can any one help me please.

Capture

#2
<?php
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="download.txt"');
    //Your Code to Put File Contents Here
?>
1 Like
#3

Thanks for your answer ; But would you explain more because i don’t understand in which part i should add your code .

#4

we don’t know either, as you did not post any code.

#5

I have a feeling that the header() function may be something that catches out every developer at some point early on. The docs have some helpful information.

https://www.php.net/manual/en/function.header.php


Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>