Php file download instance code. Php Tutorial file download instance code functionxiazai ($ file_dir, $ file_name) parameter description: file_dir: file directory file_name: file name {$ file_dirchop ($ file_dir ); remove php Tutorial file download instance code
Function xiazai ($ file_dir, $ file_name)
// Parameter description:
// File_dir: Directory of the file
// File_name: file name
{
$ File_dir = chop ($ file_dir); // remove unnecessary spaces in the path
// Obtain the path of the object to be downloaded
If ($ file_dir! = '')
{
$ File_path = $ file_dir;
If (substr ($ file_dir, strlen ($ file_dir)-1, strlen ($ file_dir ))! = '/')
$ File_path. = '/';
$ File_path. = $ file_name;
}
Else
$ File_path = $ file_name;
// Determine whether the object to be downloaded exists
If (! File_exists ($ file_path ))
{
Alert ('sorry, the file you want to download does not exist ');
Return false;
}
$ File_size = filesize ($ file_path );
Header ("Content-type: application/octet-stream; charset = gbk ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length: $ file_size ");
Header ("Content-Disposition: attachment; filename =". $ file_name );
$ Fp = fopen ($ file_path, "r ");
$ 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 );
Return true;
}
Worker file download instance code function xiazai ($ file_dir, $ file_name) // parameter description: // file_dir: Directory of the file // file_name: file name {$ file_dir = chop ($ file_dir); // remove path...