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 CodeThe code is 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);
}
?>
You can also look at the more detailed code for this comment:
Copy the Code code as follows:
File download, download a picture
$file _name= "Angel.mp3";
$file _name= "Bjnihao.jpg"; The Chinese program fails to complete the download prompt: The file does not exist
transcode Files (php file functions are older to convert Chinese code to gb2312)
Iconv-convert string to requested character encoding
by www.php.net
$file _name=iconv ("Utf-8", "gb2312", $file _name);
Set file download path (relative path)
$file _path= "./dowm/". $file _name;
Using absolute paths
$file _path=$_server[' Document_root ']. " /http/dowm/". $file _name;
Open the file---First judge the operation
if (!file_exists ($file _path)) {
echo "file does not exist";
return; Direct exit
}
Present--Open file
$FP =fopen ($file _path, "R");
Get File size
$file _size=filesize ($file _path);
Response headers required for HTTP download
Header ("Content-type:application/octet-stream"); The returned file
Header ("Accept-ranges:bytes"); Returns by byte size
Header ("Accept-length: $file _size"); Return file size
Header ("content-disposition:attachment; Filename= ". $file _name);//The Client's popup dialog box, the corresponding file name
Returning data to the client
Set size output
$buffer = 1024;
In order to download security, we'd better make a file byte read counter
$file _count=0;
Determines whether the file pointer is to the end of the file (read whether the file ends)
while (!feof ($fp) && ($file _size-$file _count) >0) {
$file _data=fread ($fp, $buffer);
Count the number of bytes read
$file _count+= $buffer;
Return part of the data to the browser
echo $file _data;
}
Close File
Fclose ($FP);
?>
Encapsulation function:
/*
Encapsulation function:
Parameter description----$file _name: File name
$file _sub_dir: Sub-path to file download
*/
function File_dowm ($file _name, $file _sub_dir) {
File transcoding
$file _name=iconv ("Utf-8", "gb2312", $file _name);
Using absolute paths
$file _path=$_server[' Document_root ']. " $file _sub_dir ". $file _name;
Open the file---First judge the operation
if (!file_exists ($file _path)) {
echo "file does not exist";
return; Direct exit
}
Present--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 download
Header ("Content-type:application/octet-stream"); The returned file
Header ("Accept-ranges:bytes"); Returns by byte size
Header ("Accept-length: $file _size"); Return file size
Header ("content-disposition:attachment; Filename= ". $file _name);//The Client's popup dialog box, the corresponding file name
Returning data to the client
Set size output
$buffer = 1024;
In order to download security, we'd better make a file byte read counter
$file _count=0;
Determines whether the file pointer is to the end of the file (read whether the file ends)
while (!feof ($fp) && ($file _size-$file _count) >0) {
$file _data=fread ($fp, $buffer);
Count the number of bytes read
$file _count+= $buffer;
Return part of the data to the browser
echo $file _data;
}
Close File
Fclose ($FP);
}
FILE_DOWM ("Bjnihao.jpg", "/http/dowm/");
?>
Another code:
Copy the 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);
}
}