Download a file using php script

Download a file using php script

download a file using php script

I want to do if user click on that link the save window should come up on screen and user should save that file into their place. for that I want a PHP script, if any 1​. In this chapter we will teach you how to create and write to a file on the server. The file will be created in the same directory where the PHP code resides. zip and text/rtf, text/plain it's acting weird. By weird I mean: It downloads the full index.php file, with the downloaded file contents inside of it. download a file using php script

Mo Beigi

So I recently added a download.php script to my website so that I could force downloads of files instead of having users access them through an indexed directory or through their browser.

I found various scripts online but none of them were as clean as I’d have liked them to be so I wrote my own simple script after a bit of research.

In my setup, the download.php file sits at the root of my website and the filevault folder sites one level higher on the web server. This setup ensures users cannot hotlink to files or directly access them, the script must be used. A benefit of this is that you can add restrictions like allowing a file to be accessed by people from a particular country or by those who have a certain cookie set. If you do not have access to the directory above your websites root directory then you are forced into putting your filevault at the websites root directory.

This is the simple PHP File download script:

*  Download File script
*  Aurthor: Mo Beigi
*  Url: https://mobeigi.com
*  Usage: /download.php?file=XXXX
set_time_limit(0);// disable the time limit for this script
if(!empty($_GET['file'])){
  $path_parts=pathinfo($_GET['file']);
  $file_name  =$path_parts['basename'];
  $path_to_filevault_dir="../filevault";
  //Build absolute file path
  $path_to_file  =$path_to_filevault_dir.'/'.$file_name;
  //Check if file exists
  if(!file_exists($path_to_file)){
    print'<div>Invalid file specified. Please notify the webmaster if you think this is a mistake.</div>';
    exit(1);
  //Check if file is readable
  if(!is_readable($path_to_file)){
    print"An error has occurred. Please notify the webmaster.";
    exit(1);
  # detect MIME type (http://stackoverflow.com/a/32092523/1800854)
  $finfo=finfo_open(FILEINFO_MIME_TYPE);
  header('Content-Type: '.finfo_file($finfo,$path_to_file));
  $finfo=finfo_open(FILEINFO_MIME_ENCODING);
  header('Content-Transfer-Encoding: '.finfo_file($finfo,$path_to_file));
  header('Content-disposition: attachment; filename="'.basename($path_to_file).'"');
  readfile($path_to_file);
  print"'file' parameter is missing and is required.";

Usage

The following link would force the download of that_file.txt

https://somewebsite.com/download.php?file=that_file.txt

 

Demo

You can also download the above script (using the script!):
Download download.php Script

Источник: https://mobeigi.com/blog/programming/simple-php-file-download-script/

Download a file using php script

3 thoughts to “Download a file using php script”

Leave a Reply

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