A simple php file download source code, although not support the continuation of the breakpoint, but can meet some common needs. PHP download files can actually be implemented with a tag, such as Magento-1.8.1.0.zip. However, some browsers can recognize the format, such as. txt,.html,.pdf, and then use Abc.txt presumably also know what will happen.
Copy the Code code as follows:
/**
* 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, which is the name after the download
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);
The header to use 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);
}
?>
Source code for PHP mandatory file download
To provide users with mandatory file download function.
Copy the 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. "Download file" So simple is worth saying? Of course it's not as simple as imagination. For example, you want customers to fill out a form, you can download a file, your first thought must be "Redirect" method, first check whether the form has been completed and complete, and then the URL refers to the file, so that customers can download, but if you want to do a "online shopping" e-commerce site , consider the 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 the Code code as follows:
$file _name = "Info_check.exe";
$file _dir = "/public/www/download/";
if (!file_exists ($file _dir. $file _name)) {//check if 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, then the source code will change a little, the program is as follows:
Copy the 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, it is important to note that the header information is equivalent to the file information high-speed browser, and then to download the information on the browser to the attachment. Therefore, if the view page must not have any content in the MVC mode application, the content of the view page will be downloaded along with the contents of the file, causing the downloaded file to become unused.
Here is my program:
Copy CodeThe code is 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. Never have any content (including the following fixed information:
Untitled DocumentOtherwise, this information will be downloaded to the download file, causing the file to not be used.
http://www.bkjia.com/PHPjc/767608.html www.bkjia.com true http://www.bkjia.com/PHPjc/767608.html techarticle a simple php file download source code, although not support the continuation of the breakpoint, but can meet some common needs. PHP download file in fact with a tag can be achieved, such as a-hr ...