PHP file Download Small instance _php Tutorial

Source: Internet
Author: User
This file download instance is very detailed he is combining the header function with the while Fread function to read out the file and then send to the client, a standard file download instance.

A small instance of a php file download
/*======================================================
$FileName is the file name, must pass
The $FilePath is the file path. Optional, can be a relative path or an absolute path
The path can only be composed of English and data, not with Chinese
If you have any questions, please contact the blogger to indicate
======================================================*/

The code is as follows Copy Code

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

if (substr ($FilePath, 0, 1) = = '/') {//Determine if absolute path

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

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

$FilePath = $FilePath. ' /';

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

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

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

$FilePath = $FilePath. " /";

}else{

$FilePath = "";

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

echo "Download failed: The file to be downloaded was not found";

}
/*================================================
Send 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);//The Client's popup dialog box, corresponding to the file name

/*================================================
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);
}

?>

Summarize

This download example and support the Chinese dissemble, the file at the beginning of the UFT8 encoding conversion.

http://www.bkjia.com/PHPjc/633123.html www.bkjia.com true http://www.bkjia.com/PHPjc/633123.html techarticle This file download instance is very detailed he is combining the header function with the while Fread function to read out the file and then send to the client, it is a standard file download real ...

  • Related Article

    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.