Tagged: php response download example

PHP Force File Download HTML button code 0

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

<a href="alive_product_file.txt" download="invoice.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://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=<?php echo $attach['file_name'];?>" class="btn btn-sm btn-raised doc-download" title="<?php echo substrwords($attach['file_name'],10);?>"><i class="material-icons">file_download</i>Download</a>
    </p>
</div>

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.