How to download php File: Php file download

Source: Internet
Author: User
Tags header php file

<?php
/** Author Zhangzhaoyu 2012-9-13 2:28:18 **/
/**
*
* @param unknown_type $file _name
* @param unknown_type $file _sub_path "xxx/xxx/"
*
*/
function Down_file ($file _name, $file _sub_dir) {
$file _path = $_server["Document_root"]. $file _sub_dir. $file _name;
if (!file_exists ($file _path)) {
echo "File not exist!";
echo $file _path;
return;
}
$file = fopen ($file _path, "R");
$file _size = filesize ($file _path);
The return file
Header ("Content-type:application/octet-stream");
Return by bytes
Header ("Accept-ranges:bytes");
Return the size of the file
Header ("Accept-length:"). $file _size);
Return the name of the file pop-up download box corresponding to the filename
Header ("content-disposition:attachment; Filename= ". $file _name);
Sending data to clients
$buffer = 1024;
$file _count = 0;
while (!feof ($file) && (($file _size-$file _count)) > 0) {
$file _data = fread ($file, $buffer);
$file _count + + $buffer;
echo $file _data;
}
Fclose ($file);
Exit ();
}
$file _name = $_request["filename"];
Down_file ($file _name, "/helloworld/project/up/");
?> This article links http://www.cxybl.com/html/wlbc/Php/20130326/37397.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.