Hello,
How can I export HTML file using PHP?

lets say that I have variable:

$var="
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>

<html>
<head>
	<title>Untitled</title>
</head>

<body>

<a href='http://www.php.com'>myphp</a> best <b>php</b> website <h1>ever!!!</h1> php and myphp or phpme - <u>php!</u>!

</body>
</html>
";

and now I want to export ("save file as") this data to HTML file (text.html). Is it possible, and if yes - how?

    [man]header[/man]("Content-Type: application/octet-stream");

      Oh, that it? only this line?
      where do I put the content of the file and where I choose its name?

        Echo as for anything that shows up in the browser. The header does the trick. Add name parameter to content-type header and also provide a content-disposition header with a filename attribute.

          Hi johanafm,
          I'm sorry but I didn't really understand your post. can you please explain to me where do I define the data I want to save in my file, and where do I choose its name...?

            Are you just wanting to save the data to an HTML file on the server?

            If so, just use [man]file_put_contents/man to write the string to a file.

              Yes, this is, more or less, what I was looking for, But instead of saving it on the server I want to be able to save it one my PC.

                Well then it's not what you're looking for. Writing a string to a file on the server is a completely different issue than manipulating a browser to open a "Save As" dialog.

                For the latter, you'll want to output a header such as the one Weedpacket suggested and then output the data.

                  Why its different?
                  After all the function that you wrote me before create a file and save the content inside it, like I need. The only thing that let is to send me the file so I could download it.
                  'if I'll add

                  header("Content-Type: application/octet-stream"); 

                  will it send me the file $file = 'people.txt'; (accourding to the example: http://il2.php.net/file_put_contents) ?

                    It's different because it has nothing to do with what you want. Creating a file on the server and writing data to it has nothing to do with outputting data to a browser and causing it to prompt the user to save this data to a file.

                    [man]file_put_contents/man is for creating a file on the server. This won't help you with what you're trying to achieve.

                    What you want to do is output the appropriate header, and then just output the data as you normally would. Instead of displaying it, the browser should open a "Save As" dialog or something of that nature.

                      OK
                      But how adding header("Content-Type: application/octet-stream"); will answer my problem? how the header will know which content to send?

                        The header doesn't send anything. Your script does, just like it's your script that sends the header (thanks to your web server). How do you know what to send when you want to display things to the user, rather than having them download it? There is no difference.

                          so there is no way to send temp.html to myself?

                          <?PHP
                          $file = 'temp.html';
                          $current = file_get_contents($file);
                          file_put_contents($file, $current, FILE_APPEND);
                          ?>
                          

                            Of course there is

                            <?PHP
                            $file = 'temp.html';
                            $current = file_get_contents($file);
                            echo $current;
                            ?> 
                            

                              by "sending" I ment - sending the file to myself... 🙂
                              ?

                                answer: same as in last post.

                                but if you want to ALSO prevent others from getting this data, you'd need to add authentication to your page. it DOES however get the data to you. By default, it doesn't care about who's on the other end.

                                  Write a Reply...