php Download File Source Code of Forces any file format Download

  • 2021-06-28 08:54:25
  • OfStack

A simple php file downloads the source code. Although breakpoint continuation is not supported, it can meet some common requirements.php download files can be achieved with an a tag, such as < a href="web/magento-1.8.1.0.zip" > magento-1.8.1.0.zip < /a > .But there are a few formats that browsers can recognize, such as.txt,.html,.pdf, and so on. < a href="web/abc.txt" > abc.txt < /a > You must know what's going to happen.


<?php 
/**
 *  File Download 
 *
**/
header("Content-type:text/html;charset=utf-8");
download('web/magento-1.8.1.0.zip', 'magento download '); 
function download($file, $down_name){
 $suffix = substr($file,strrpos($file,'.')); // Get File Suffix 
 $down_name = $down_name.$suffix; // The new file name is the downloaded name 
 // Determine whether a given file exists or not  
 if(!file_exists($file)){
  die(" The file you are downloading no longer exists, it may have been deleted ");
 } 
 $fp = fopen($file,"r");
 $file_size = filesize($file);
 // Headers needed to download files 
 header("Content-type: application/octet-stream");
 header("Accept-Ranges: bytes");
 header("Accept-Length:".$file_size);
 header("Content-Disposition: attachment; filename=".$down_name);
 $buffer = 1024;
 $file_count = 0;
 // Return data to browser  
 while(!feof($fp) && $file_count < $file_size){
  $file_con = fread($fp,$buffer);
  $file_count += $buffer;
  echo $file_con;
 } 
 fclose($fp);
}
?>

Source code for PHP mandatory file download

Provides users with mandatory file download capabilities.


/********************
*@file - path to file
*/
function force_download($file)
{
if ((isset($file))&&(file_exists($file))) {
header("Content-length: ".filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$file");
} else {
echo "No file selected";
}
}

Are you sure you'll laugh at me for "downloading files" so easy to say?Of course, it's not as simple as you think.For example, if you want customers to fill out a form before they can download a file, your first idea is to use the "Redirect" method. First check that the form is complete and complete, and then point the web address to the file so that customers can download it, but if you want to make an e-commerce website about "online shopping", consider security issues.You don't want users to download the file directly by copying the web address. I suggest you use PHP to read the actual file directly and download it.The procedure is as follows:


$file_name = "info_check.exe";
$file_dir = "/public/www/download/";
if (!file_exists($file_dir . $file_name)) { // Check file existence 
echo " File not found ";
exit;
} else {
$file = fopen($file_dir . $file_name,"r"); //  Open File 
//  Enter File Label 
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir . $file_name));
Header("Content-Disposition: attachment; filename=" . $file_name);
//  Output File Content 
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;
} 

If the file path is "http" or "ftp" web address, the source code will be changed slightly, as follows:


$file_name = "info_check.exe";
$file_dir = "https://www.ofstack.com/";
$file = @ fopen($file_dir . $file_name,"r");
if (!$file) {
echo " File not found ";
} else {
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=" . $file_name);
while (!feof ($file)) {
echo fread($file,50000);
}
fclose ($file);
} 

This allows you to output files directly using PHP.

However, 1 must note: Header information is equivalent to a high-speed file information browser first, then the information on the browser is downloaded to the attachment.Therefore, if there is no content on page 1 of view in MVC mode application, otherwise, the content of page 1 of view will be downloaded with the content 1 of the file, which will make the downloaded file unusable.

Here is my program:


   public function downloadAction()
    {
        if (isset($_GET['mriID']))
    {
    $this->view->mriID=(get_magic_quotes_gpc())?$_GET['mriID']:addslashes($_GET['mriID']);
    }
        if (isset($_GET['dicomID']))
    {
    $this->view->dicomID=(get_magic_quotes_gpc())?$_GET['dicomID']:addslashes($_GET['dicomID']);
    }
       if (isset($_GET['JPGID']))
    {
    $this->view->JPGID=(get_magic_quotes_gpc())?$_GET['JPGID']:addslashes($_GET['JPGID']);
    }
    $dicomfile=new dicomfile(); 
    $jpgfile=new jpgfile();
    $mri=new mri();
    if($this->view->dicomID)
    {
    $filename=$dicomfile->find($this->view->dicomID)->toArray();
    $filename=$filename[0]['filename'];
    }   
    else if($this->view->JPGID)
    {
      $filename=$jpgfile->find($this->view->JPGID)->toArray();
      $filename=$filename[0]['JPGname'];
    }
    $dir=$mri->find($this->view->mriID)->toArray();
    $dir=$dir[0]['dicom_path'];
    $file=$dir.'/'.$filename;
    if (!file_exists($file))
    {
    echo "the file does not exist!";
    exit();
    }
      $file_size=filesize($file);
           header("Content-type: application/octet-stream");
           header("Accept-Ranges: bytes");
           header("Accept-Length:". $file_size);
           header("Content-Disposition: attachment; filename=".$filename); 
           $fp=fopen($file,"r");
      if (!$fp)
    echo "can't open file!";
       $buffer_size=1024;
    $cur_pos=0;
    while (!feof($fp)&&$file_size-$cur_pos>$buffer_size)
    {
    $buffer=fread($fp,$buffer_size);
    echo $buffer;
    $cur_pos+=$buffer_size;
        }
    $buffer=fread($fp,$file_size-$cur_pos);
    echo $buffer;
    fclose($fp);  
    }

At this point, page 1 of download.phtml must be completely blank.Never have anything (including the following fixed information): < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" / >
< title > Untitled Document < /title > Otherwise, the information will be downloaded to the download file, which will make the file unusable.


Related articles: