-1

I want to download html page using php and ajax. For now i am using the link like this:

<a href="https://www.example.com/page.html" download>Download</a>

Is there a way to do this using php and ajax?

Thank you in advance for your answer.

Note: This answer is not worked for me i have tryed: How to download HTML using PHP?

AlwaysStudent
  • 1,354
  • 18
  • 49
  • 1
    Does this answer your question? [How to download HTML using PHP?](https://stackoverflow.com/questions/1792372/how-to-download-html-using-php) – MorganFreeFarm Dec 12 '19 at 07:57
  • @Azzo, do you want to save the file to user's device when they click on download button, is that what you mean by download ? – OMi Shah Dec 12 '19 at 08:21
  • @OMiShah Yes i want this. When user click the button then the `href="page.html" `should download his/her device. – AlwaysStudent Dec 12 '19 at 08:22
  • isn't using download attribute, file getting downloaded? what issue you're facing currently – OMi Shah Dec 12 '19 at 08:28
  • @OMiShah You know that the download button not working some browsers. I want to make it more usefully if possible with php. – AlwaysStudent Dec 12 '19 at 08:30
  • hmm, yes, i am aware of that. For that you don't need to use ajax, just use php. Check my answer. – OMi Shah Dec 12 '19 at 08:31

3 Answers3

0

Hope this will help you...

<?php

     //Link to download file...
     $url = "http://example.com/test.php";

     //Code to get the file...
     $data = file_get_contents($url);

     //save as?
     $filename = "test.html";

     //save the file...
     $fh = fopen($filename,"w");
     fwrite($fh,$data);
     fclose($fh);

     //display link to the file you just saved...
     echo "<a href='".$filename."'>Click Here</a> to download the file...";

    ?>
Hassan Fayyaz
  • 685
  • 1
  • 9
  • 15
0

you can try like this in case of secured (https) protocol's html page

$context = stream_context_create(
    array(
        "http" => array(
        "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
        )
    )
);


$contents = file_get_contents("https://www.example.com/page.html", false, $context);

file_put_contents("download.html", $contents);
Anitha
  • 135
  • 1
  • 8
0

You can force download when a user clicks on the download link.

download.php file on your server

<?php

$fileName = $_GET['filename'];

/* make sure to filter the file so that one could not download any other restricted file. Or better, keep your publicly downloadable files in a folder called "public_files" and store files in that */

$filesFolder = "public_files";

if (file_exists($filesFolder . "/" . $fileName)) {

    /* from https://stackoverflow.com/a/3476444/5882307 */

    $fileSize = filesize($filesFolder . "/" . $fileName);

    // Output headers.
    header("Cache-Control: private");
    header("Content-Type: application/stream");
    header("Content-Length: " . $fileSize);
    header("Content-Disposition: attachment; filename=" . $fileName);

    // Output file.
    readfile($filePath);
    exit();
} else {
    die('The provided file path is not valid.');
}
?>

And then you can link your file as :

<a href="download.php?filename=yourfile.html">Download</a>
OMi Shah
  • 5,768
  • 3
  • 25
  • 34