PHP Force File Download HTML button code

Today, We want to share with you PHP Force File Download HTML button code.In this post we will show you php code for downloading a file from folder, hear for Download File from Mysql Database using php we will give you demo and example for implement.In this post, we will learn about php code for downloading a file from database with an example.

PHP Force File Download HTML button code

There are the Following The simple About PHP Force File Download HTML button code Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP script to download a file from files/ directory, so the how to download a file from the server using php code for this example is following below.

Also Read This πŸ‘‰   Angular 9/8 Material Input Box Examples

Example 1: html download button code

html file download example

Force the Download of a File with HTTP Headers and PHP

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="invoice.txt"

PHP force download file with the response helper method

function force_php_Download($my_filename, $type = "application/octet-stream") {
    header('Content-Type: '.$type.'; charset=utf-8');
    header('Content-Disposition: attachment; filename="'.$my_filename.'"');

downloading pdf file using html code

force_php_Download("product_list.html", "text/html");
echo file_get_contents("");

Alternative : how to make a file downloadable in php

<a href="alive_product_file.txt"><i class="material-icons">file_download</i>download</a>

Example 2: How to Force Download Files Using PHP


Download Link Without Filename

set_time_limit(0); // disable the time any custom file limit for this script
$path = ""; // change the path to fit your domain name websites document structure
$dastinationl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['file']); // simple file name validation
$dastinationl_file = filter_var($dastinationl_file, FILTER_SANITIZE_URL); // Delete (more) invalid your remove characters
$fullPath = $path.$dastinationl_file;
if ($filedata = fopen ($fullPath, "r")) {
    $filesize = filesize($fullPath);
    $get_path = pathinfo($fullPath);
    $ext = strtolower($get_path["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=\"".$get_path["basename"]."\""); // use 'attachment' to force a file download
        // put your more headers Like Images, Video, PNG, JPEG for other content types here
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$get_path["basename"]."\"");
    header("Content-length: $filesize");
    header("Cache-control: private"); //use this to open your files directly in File Handaling
    while(!feof($filedata)) {
        $data_buffer = fread($filedata, 2048);
        echo $data_buffer;
fclose ($filedata);


<div class="caption">
      <a href="download.php?file=" class="btn btn-sm btn-raised doc-download" title=""><i class="material-icons">file_download</i>Download</a>

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read This πŸ‘‰   How to generate a html page dynamically using PHP?

Related Keywords :PHP Force Download a pdf File PHP – Force Download File using response helper, PHP force download file with the response helper method, How to Force the Download of a File with HTTP Headers and PHP


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Force File Download HTML button code.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.