Download if only ordinary file download, there is no need to use PHP, with a <a href= ' Http://xx.xx.com/xx.tar ' ></a> can, but sometimes to keep files confidential, can only be downloaded to some people, Obviously not be able to tell others about the link, if this is not the effect of confidentiality.
The following function is a PHP-written file to download the function, it is a section of the file read out, and then sent to the client.
function Download ($file _dir, $file _name)
Parameter description:
File_dir: directory where files reside
file_name: filename
{
$file _dir = Chop ($file _dir);//Remove extra spaces from the path
The path to the file to download
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 if the file you want to download exists
if (!file_exists ($file _path))
{
Echo ' Sorry, the file you are downloading does not exist. ';
return false;
}
$file _size = filesize ($file _path);
Header ("Content-type:application/octet-stream");
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;
}