Tagged: php download file script

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.

How to Force Download Files Using Laravel 5.7 0

Laravel Force Download Files

Today, We want to share with you How to Force Download Files Using Laravel 5.7.In this post we will show you Laravel 5.6 Force Download a pdf File, hear for Laravel 5.7 – Force Download File using response helper we will give you demo and example for implement.In this post, we will learn about Laravel 5.7 force download file with the response helper method with an example.

How to Force Download Files Using Laravel 5.7

There are the Following The simple About How to Force Download Files Using Laravel 5.7 Full Information With Example and source code.

As I will cover this Post with live Working example to develop downloading pdf file using html code, so the Force File Download With Laravel for this example is following below.

Example 1:Force File Download With Laravel

HTML Blade View Files

<a href="{{ url('') }}/download/{{$attach->file_name}}" class="btn btn-primary btn-sm btn-raised doc-download" title="{{$your_attach_object->file_name}}"><i class="fa fa-download"></i> Download</a> </p>

Laravel Define Routes

project-name-system/routes/web.php

Route::get('/download/{file}', '[email protected]');

Laravel Create a Controller

app/Http/Controllers/HomeController.php

public function download($file_name) {
    $file_path_full =base_path()."/img/atteach/".$file_name;
    $file_path =pathinfo(base_path()."/img/atteach/".$file_name);

    $basename = $file_path['basename'];
    $path = $file_path['dirname'];
    return response()->download($file_path_full, $basename, ['Content-Type' => 'application/force-download']);
  }

Example 2: Forcing File Downloads in PHP Laravel

  return response()->download($file, "product-2019 - invoice.pdf");

  or

  Route::post('downloads', function (Request $request) {
    //Laravel cache the file
    $file = $request->file('photo');

    //Laravel create a new filename. getClientOriginalExtension() for the (png, jpg) file extension
    $get_filename = 'User-photo-Profiles' . time() . '.' . $file->getClientOriginalExtension();

    //Laravel save/store to storage/app/photos as the new $get_filename
    $storage_path = $file->storeAs('photos', $get_filename);

    dd($storage_path);
});

Example 3: Laravel 5.5 – Force Download File using response helper

Laravel define Route

routes/web.php

Route::get('fileforceDonwload', '[email protected]');

Add Controller Method

app/Http/Controllers/HomeController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
    public function fileforceDonwload()
    {
        $pathTo_orignalFile = public_path("product_invoice_2020.pdf");
        $rename_file_name = time().'.pdf';
        $headers = ['Content-Type: application/pdf'];
        return response()->download($pathTo_orignalFile, $rename_file_name, $headers);
    }

}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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

I hope you get an idea about How to Force Download Files Using Laravel 5.7.
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.