Download HTML page Using PHP and Ajax

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?

4 answers

  • answered 2019-12-12 08:04 Hassan Fayyaz

    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...";
    
        ?>
    

  • answered 2019-12-12 08:05 Anitha

    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);
    

  • answered 2019-12-12 08:26 OMi Shah

    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>
    

  • answered 2019-12-12 09:39 DeltaForce

    You can use donwnload.js

    The example Code :

    Ajax and including downloadjs

    <script type="text/javascript" src="js/downloadjs.js"></script>
    <script type="text/javascript"> 
         function dlHTML(){ 
            $.ajax({
            url: "yourhtmlpage.html", 
            success: download.bind(true, "text/html", "yourhtmlpagename.html")
        });
        }  
    </script>
    

    html

    <button onclick="dlHTML()"></button>