Posted inTechnology / php / Programming

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.

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

file_downloaddownload

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

<a href="download.php?file=" class="btn btn-sm btn-raised doc-download" title="">file_downloadDownload

Angular 6 CRUD Operations Application Tutorials

Read :

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype