PHP download File source code (force arbitrary file format download) _php tips

Source: Internet
Author: User
Tags fread php download ranges

A simple php file download source code, although does not support the extension of the breakpoint, but can meet some common needs. PHP download file in fact with a tag can be implemented, such as <a href= "Web/magento-1.8.1.0.zip" >magento-1.8.1.0.zip</a>. But some browsers can recognize the format, such as. Txt,.html,.pdf and so on, and then use <a href= "Web/abc.txt" >abc.txt</a> presumably also know what will happen.

Copy Code code as follows:

<?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; New file name, is the name after the download

To determine whether a given file exists or not
if (!file_exists ($file)) {
Die ("The file you want to download no longer exists, may be deleted");
}
$fp = fopen ($file, "R");
$file _size = filesize ($file);
Headers needed to download the file
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;
Returning data to the browser
while (!feof ($fp) && $file _count < $file _size) {
$file _con = fread ($fp, $buffer);
$file _count + + $buffer;
echo $file _con;
}
Fclose ($FP);
}
?>

PHP mandatory file Download source code

To provide users with mandatory file download capabilities.

Copy Code code as follows:

/********************
* @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";
}
}

You're going to laugh at me. "Downloading files" is worth saying so easily? Of course it's not as simple as imagination. For example, if you want the customer to fill out a form, you can download a file, your first idea must be "Redirect" method, first check whether the form has been completed and complete, and then the URL to the file, so that customers can download, but if you want to do a "online shopping" E-commerce site , consider security issues, you do not want users to directly copy the URL to download the file, I suggest you use PHP directly read the actual file and then download the method to do. The procedure is as follows:

Copy Code code as follows:

$file _name = "Info_check.exe";
$file _dir = "/public/www/download/";
if (!file_exists ($file _dir. $file _name)) {//check whether the file exists
echo "File not found";
Exit
} else {
$file = fopen ($file _dir. $file _name, "R"); Open File
Input 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 contents
Echo fread ($file, FileSize ($file _dir. $file _name));
Fclose ($file);
Exit
}

If the file path is "http" or "FTP" URL, the source code will change a little bit, the program is as follows:

Copy Code code as follows:

$file _name = "Info_check.exe";
$file _dir = "http://www.jb51.net/";
$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 the file directly with PHP.

However, be sure to note that header information is equivalent to first file information high-speed browser, and then the browser to download the information to the attachment. Therefore, if the view page must have no content in the MVC mode application, the contents of the View page will be downloaded along with the contents of the file, causing the downloaded file to not be used.

Here's my program:

Copy Code code as follows:

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, the download.phtml page must be completely blank. Do not have any content (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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title> Otherwise, this information will be downloaded to the download file, causing the file to not be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.