AWK examples

Last updated on January 10th, 2023 at 01:57 pm

There are different ways to download a file. Easiest method is to use simple HTML like the one below.

<a href="file_to_download.txt">Download</a>

With a little help from Javascript and a way to hide the location of files (If you add some encryption techniques, not explained in this tutorial) you can make use PHP . All we are doing here is create a simple Javascript function and use window.location to call the PHP file.
Let us make a javascript function named download(), Create a html file with the name getfile.html and add the below javascript and html.

<script type="text/javascript">
function download(filename)
{
 window.location="Download.php?path="+filename;;
}
</script>

Now in order to trigger the download, use the html snippet below to call the javascript function.

<a href="javascript:download('myfile.txt')">Download</a>

As you can see javascript function calls the PHP script Download.php that will in turn trigger a call to initiate a file download, in this case the file going to get downloaded will be myfile.txt

<?php
   header("Content-Type: application/octet-stream");
   header("Content-Disposition: attachment); 
   filename=".$_GET['path']);
   readfile($_GET['path']);
?>

Make sure you copy the getfile.html and Download.php file in the same location. You can always modify the file path accordingly. Happy Coding!!! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *