0

Below code works perfectly to download the .html file of a current PHP web page

$filename = 'filename.html';
header('Content-disposition: inline; filename=' . $filename);
header('Content-type: text/html');

Is there any way to save file in some specific location instead the download response.

Ex: if i want to save this filename.html file in location /export/www/html/myproject/var/htmlpages

Tried below logic as alternative also:

ob_start();
file_put_contents('myfile.html', ob_get_contents());

But its generating empty file

Akhil Gupta
  • 187
  • 3
  • 13
  • not sure if I understood your question. the client/user-agent download it where he wants – Federkun Dec 05 '16 at 10:52
  • I need to put current page html content in this file. And don't want to use $html = file_get_contents('my-domain.com.'); – Akhil Gupta Dec 05 '16 at 10:58
  • I saw content disposition works well. Don't want user to get the download request. Instead if it can save internally in some location. Is it possible? – Akhil Gupta Dec 05 '16 at 10:58
  • Possible duplicate of [how to redirect STDOUT to a file in PHP?](http://stackoverflow.com/questions/937627/how-to-redirect-stdout-to-a-file-in-php) – Federkun Dec 05 '16 at 11:00
  • Earlier tried this one also but it's generating empty file. – Akhil Gupta Dec 05 '16 at 11:10
  • of course it generate an empty file. you should start with `ob_start`, then you print what you want in the file, and only then you call `ob_get_contents`. – Federkun Dec 05 '16 at 11:21

1 Answers1

1

Use fopen() to write a file in a specific directory instead of download response like:

<?php
$fp = fopen('data.txt', 'w');    // here you can pass the directory path + filename
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
?>

fwrite() doc

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • I need to put current page html content in this file. And don't want to use $html = file_get_contents('http://my-domain.com.'); Please suggest any alternative. – Akhil Gupta Dec 05 '16 at 10:55