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);
}
?>
You can also take a look at this comment for more detailed code:
Copy Code code as follows:
<?php
File download, download a picture
$file _name= "Angel.mp3";
$file _name= "Bjnihao.jpg"; Chinese program cannot complete download prompt: File does not exist
transcoding the file (php file function is older need to change the Chinese code into gb2312)
Iconv-convert string to requested character encoding
by www.jb51.net
$file _name=iconv ("Utf-8", "gb2312", $file _name);
Set file download path (relative path)
$file _path= "./dowm/". $file _name;
Use absolute path
$file _path=$_server[' Document_root ']. " /http/dowm/". $file _name;
To open a file---first determine the operation
if (!file_exists ($file _path)) {
echo "file does not exist";
return; Exit directly
}
exist--Open file
$FP =fopen ($file _path, "R");
Get File size
$file _size=filesize ($file _path);
Response headers required for HTTP downloads
Header ("Content-type:application/octet-stream"); The file returned
Header ("Accept-ranges:bytes"); return by byte size
Header ("Accept-length: $file _size"); Return file size
Header ("content-disposition:attachment; Filename= ". $file _name);//Here The client's pop-up dialog box, the corresponding filename
Returning data to the client
Set size output
$buffer = 1024;
In order to download security, we'd better do a file byte read counter
$file _count=0;
Determine if the file pointer is at the end of the file (read the file is finished)
while (!feof ($fp) && ($file _size-$file _count) >0) {
$file _data=fread ($fp, $buffer);
Count the number of bytes read
$file _count+= $buffer;
Return some data to the browser
echo $file _data;
}
Close File
Fclose ($FP);
?>
Encapsulate function:
<?php
/*
Encapsulate function:
Parameter description----$file _name: File name
$file _sub_dir: Child paths for file downloads
*/
function File_dowm ($file _name, $file _sub_dir) {
File Transfer Code
$file _name=iconv ("Utf-8", "gb2312", $file _name);
Use absolute path
$file _path=$_server[' Document_root ']. " $file _sub_dir ". $file _name;
To open a file---first determine the operation
if (!file_exists ($file _path)) {
echo "file does not exist";
return; Exit directly
}
exist--Open file
$FP =fopen ($file _path, "R");
Get File size
$file _size=filesize ($file _path);
/*
Here can be set more than how big can not download
if ($file _size>50) {
echo "File too large to download";
return;
}*/
Response headers required for HTTP downloads
Header ("Content-type:application/octet-stream"); The file returned
Header ("Accept-ranges:bytes"); return by byte size
Header ("Accept-length: $file _size"); Return file size
Header ("content-disposition:attachment; Filename= ". $file _name);//Here The client's pop-up dialog box, the corresponding filename
Returning data to the client
Set size output
$buffer = 1024;
In order to download security, we'd better do a file byte read counter
$file _count=0;
Determine if the file pointer is at the end of the file (read the file is finished)
while (!feof ($fp) && ($file _size-$file _count) >0) {
$file _data=fread ($fp, $buffer);
Count the number of bytes read
$file _count+= $buffer;
Return some data to the browser
echo $file _data;
}
Close File
Fclose ($FP);
}
FILE_DOWM ("Bjnihao.jpg", "/http/dowm/");
?>
Another code:
Copy Code code as follows:
Public Function Downloads ($name) {
$name _tmp = Explode ("_", $name);
$type = $name _tmp[0];
$file _time = Explode (".", $name _tmp[3]);
$file _time = $file _time[0];
$file _date = Date ("Y/md", $file _time);
$file _dir = Site_path. " /data/uploads/$type/$file _date/";
if (!file_exists ($file _dir. $name)) {
Header ("content-type:text/html; Charset=utf-8 ");
echo "File not found!";
Exit
} else {
$file = fopen ($file _dir. $name, "R");
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _dir. $name));
Header ("content-disposition:attachment; Filename= ". $name);
Echo fread ($file, FileSize ($file _dir. $name));
Fclose ($file);
}
}