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.

Read Also:  how to Get Database Table Prefix In WordPress?

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("http://www.wikipedia.org");

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.php

Download Link Without Filename

<?php
 
ignore_user_abort(true);
set_time_limit(0); // disable the time any custom file limit for this script
 
$path = "https://www.pakainfo.com/tools/img/atteach/"; // 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
        break;
        // put your more headers Like Images, Video, PNG, JPEG for other content types here
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$get_path["basename"]."\"");
        break;
    }
    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);
exit;

HTML Part

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

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Insert JSON data MySQL

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

Summary

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 Pakainfo.com 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.

Read Also:  Laravel Autocomplete Live Search from database

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Call Parent Class construct Before Child’s In PHP
  2. Read Also:  ASP.NET Page life cycle with Events
  3. Read Also:  How to get Main or Base URL in Laravel?
  4. Read Also:  Set Cookies Get Cookies Delete Cookies with PHP
  5. Read Also:  Wordpress Plugin uninstall hook - register_uninstall_hook()
  6. Read Also:  How to Force Download Files Using Laravel 5.7
  7. Read Also:  PHP Check duplicated value in array Example
  8. Read Also:  vuejs onchange Event - vuejs v-on change Examples
  9. Read Also:  Set Cookies Get Cookies Delete Cookies with PHP
  10. Read Also:  How to Force Download Files Using Laravel 5.7
CLOSEX