Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an empty html file with php

Tags:

html

php

I'm trying to create a blank html file through php. I wrote some code but it seems not to be working. This is my code:

            $filename = 'test.html';        
    header("Cache-Control: public");
    header("Content-Description: File Transfer");       
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/octet-stream; ");
    header("Content-Transfer-Encoding: binary");

When I click the link that would create this file, it gives me an html file but when I see the source it shows the source code of the page where i requested this.

Is there any reason why this wont work? Also, what is faster? To generate an html file everytime a user asks or to just pull a created empty file and just rename it when the user asks? Thanks!

like image 676
raygo Avatar asked Sep 01 '25 22:09

raygo


2 Answers

You may want to use the fopen function:

$file = fopen("test.html", 'w') or die("can't open file");
fclose($file);

This should create a file named test.html in the same path as the .php file that executed the code

like image 109
Shawn Northrop Avatar answered Sep 03 '25 14:09

Shawn Northrop


For those that stumbled on this question like I did for creating a fully empty file in PHP, a very short solution is doing the following:

file_put_contents('test.html', '');

To which you can simply link them to that file, to answer OP's question.

like image 30
Mike Weir Avatar answered Sep 03 '25 14:09

Mike Weir