1. php download principle, File Download source code: 1234567891011121314151617181920212223242526? Php?file_namehaha.jpg; the file to be downloaded $ file_nameiconv (UTF-8, gb2312, $ file_name); $ fpfopen ($ file_name, r +
1. php download schematic 2. File Download source code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26? Php $ file_name = ".jpg"; // file to be downloaded $ file_name = iconv ("UTF-8", "gb2312", "$ file_name "); $ fp = fopen ($ file_name, "r +"
1. php download schematic
2. File Download source code:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
$file_name="Haha.jpg";// The object to be downloaded
$file_name=iconv("utf-8","gb2312","$file_name");
$fp=fopen($file_name,"r+");// Before downloading an object, you must open the object and write it to the memory.
if(!file_exists($file_name)){// Determine whether a file exists
echo "File does not exist";
exit();
}
$file_size=filesize("a.jpg");// Determine the file size
// Returned File
Header("Content-type: application/octet-stream");
// Return in byte format
Header("Accept-Ranges: bytes");
// Returns the file size.
Header("Accept-Length: ".$file_size);
// The client dialog box appears, with the corresponding file name
Header("Content-Disposition: attachment; filename=".$file_name);
// Prevents instantaneous server load increases and reads data in Segments
$buffer=1024;
while(!feof($fp)){
$file_data=fread($fp,$buffer);
echo $file_data;
}
// Close the file
fclose($fp);
?>
|
3. Solution to file encoding problems:
If the file name is Chinese, php functions cannot recognize the Chinese file name. Generally, if the program code is UTF-8, php functions are old and can only recognize the Chinese characters encoded in gb2312, therefore, the iconv ("original encoding", "encoding to be converted", and "string to be transcoded") function can be used for transcoding.
For example, transcode a string from UTF-8 to gb2312
$ File_name = iconv ("UTF-8", "gb2312", "$ file_name ");