PHP implementation file Download (support Chinese Wennan) _php instance

Source: Internet
Author: User

Copy Code code as follows:

/*======================================================
$FileName for file name, must pass
$FilePath is the file path. Optional, can be a relative or absolute path
Paths can only be made up of English and data, not with Chinese
======================================================*/

<?php
Header ("Content-type:text/html;charset=utf-8");
if (strlen ($FileName) <=3) {echo download failed: You so downloaded the file information incorrectly "; return;}
$FileName =iconv ("Utf-8", "gb2312", $FileName);//File name format conversion to prevent Chinese garbled
Start to judge the path
if (!is_null ($FilePath) &&strlen ($FilePath) >1) {

if (substr ($FilePath, 0, 1) = = '/') {//To determine whether it is an absolute path

$FilePath =$_server[' Document_root ']. $FilePath;

}
if (substr ($FilePath,-1)!= "/") {//check last for/end

$FilePath = $FilePath. ' /';

}
if (Is_numeric ($FilePath, ": \")) {//Check for absolute path (STRPOS)

$FilePath =str_replace ("/", "\", $FilePath);

}
}elseif (strlen ($FilePath) ==1&& $FilePath!= "/") {

$FilePath = $FilePath. " /";

}else{

$FilePath = "";

}
if (!file_exists ($FilePath. $FileName)) {

echo "Download failed: The file you want to download is not found";

}
/*================================================
Send a download related header information
=================================================*/

Header ("Content-type:application/octet-stream");

Header ("Accept-ranges:bytes");//return by byte size

Header ("Accept-length: $FileSize");//Return file size

Header ("content-disposition:attachment; Filename= ". $FileName);//Here The client's pop-up dialog box, the corresponding filename

/*================================================
Start Download related
=================================================*/
$FileSize =filesize ($FilePath. $FileName);

$File =fopen ($FilePath. $FileName, "R");/Open File

$FileBuff = 512;

while ($FileSize >=0) {

$FileSize-= $FileBuff;

Echo fread ($File, $FileBuff);

}

Fclose ($File);
}
?>


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.