2019-01-20 Pujan Niroula 1 Minute(s) Read

How to Force Download File With PHP

img-force download file php.jpg

Basically, most of files such .ZIP, .EXE, .DOCX etc files are easy to download, its just matter of an hyperlink but what about PHP, HTML files?

How do you share your 'PHP file' to download in your website? If you just placed a link of PHP file it get executed. Same happens with PHP,HTML and other several file types. Some website owner choose Google Drive to store and download file but it gives bad impression to visitors.

So you must force to download instead of executing. How can you force file to download? Don't worry, today you are going to learn to download PHP file without executing.

How to Force File to Download with PHP?

Here is full code to force file to download, 

Download PHP File With PHP

<?php
$file_path = "download/file.php";

//Check File Exists or Not
if (file_exists($file_path)) {
    header('Content-Disposition: attachment; filename="'.basename($file_path).'"');
    header('Content-Type: application/octet-stream');
    header('Content-Length:'.filesize($file_path));

    //Fix IE issue
    header('Cache-Control:private');
    header('Pragma: private');

    //Read the file to download
    readfile($file_path);
} else {
    die('File Not Found');
}
?>

If you allow user to change $file_path value then your sensitive data may be downloaded by user. So use this code if user/visitors can not change $file_path else use code given below:

Store all file paths which you want to allow user to download in $allow_files extension. It will check if the given file is inside an array or not. 

<?php

//Change File Path
$file_path = "download/file.php";

//Store all files allowed to download

$allow_files = array('download/file.php','download/pujan.php','download/anotherfile.php','download/new.php');

//Check File is allowed to download or not

if (in_array($file_path, $allow_files) && file_exists($file_path)) {

    header('Content-Disposition: attachment; filename="'.basename($file_path).'"');

    header('Content-Type: application/octet-stream');

    header('Content-Length:'.filesize($file_path));

    //Fix IE issue

    header('Cache-Control:private');

    header('Pragma: private');

    //Read the file to download

    readfile($file_path);

} else {

    die('Error: File Not Found');

}

?>

Hope you liked it,

Don't forgot to share this with your friends...

Tags:php, how to,
Comments (2) Add New Comment
Resin Bound Driveways2024-05-03 7:16 PM

Link exchange is nothing else however it is simply placing the other person's webpage link on your page at appropriate place and other person will also do similar in favor of you.

Reply
php slot machine2024-02-06 9:09 AM

When he’s not writing PHP or Go code, he’s writing and modifying nice PHP articles right here at Twilio. I was working for an organization in the UK that provided £200 for well-written, technically-right articles. To chop a protracted story quick, I was the most junior particular person in the company and not one of the older engineers had been interested in the venture or using this quaint new language called PHP. The AST and OPCode combine PHP scripts with out using JIT. This includes the conversion of PHP scripts into machine language that might be easily understood by the processor. How are PHP scripts compiled? In languages with multiple dispatch, strategies are outlined by generic functions. There are three methods to execute a PHP script which might be discussed beneath. Bulletin is paid forum script but it is worth to its value. How is a PHP script executed? Aspiring PHP builders can learn about this futuristic programming device nearly immediately. You may create cell-friendly layouts and dynamic content material for a polished product that you'll be proud to show off to your prospects, customers, or traders. Whether you might be dealing with a mental well being situation or just wanting for ways to boost your effectively-being, knowing where to turn could make a significant difference in your journey toward higher mental well being.

Reply