PHP implementation File Download Simple example (code implementation file download) _php tutorial

Source: Internet
Author: User
Tags fread ranges
Copy CodeThe code is as follows:
Public Function Downloads ($name) {
$name _tmp = Explode ("_", $name);
$type = $name _tmp[0];
$file _time = Explode (".", $name _tmp[3]);
$file _time = $file _time[0];
$file _date = Date ("Y/md", $file _time);
$file _dir = Site_path. " /data/uploads/$type/$file _date/";

if (!file_exists ($file _dir. $name)) {
Header ("content-type:text/html; Charset=utf-8 ");
echo "File not found!";
Exit
} else {
$file = fopen ($file _dir. $name, "R");
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _dir. $name));
Header ("content-disposition:attachment; Filename= ". $name);
Echo fread ($file, FileSize ($file _dir. $name));
Fclose ($file);
}
}

Example 2: Code implementation file Download

The general implementation of the download is the call URL to download, but encountered IE can recognize open files can not be used in this way, such as downloading a picture, HTML page, etc., then need to be programmed to implement, the following PHP code can be resolved:

Copy the Code code as follows:
if (Empty ($_get[' FileName ']) | | empty ($_get[' Filedir ')) | | empty ($_get[' FileId '])) {
Echo '; Exit ();
}
$file _name=$_get[' FileName ';
$file _dir=$_get[' Filedir '];
$FileId =$_get[' FileId '];
$file _dir = $file _dir. " /";
if (!file_exists ($file _dir. $file _name)) {//check if file exists
echo "File not found";
Exit
} else {
$file = fopen ($file _dir. $file _name, "R"); Open File
Input File Label
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _dir. $file _name));
Header ("content-disposition:attachment; Filename= ". $file _name);
Output file contents
Echo fread ($file, FileSize ($file _dir. $file _name));
Fclose ($file);
Exit ();
}
?>

http://www.bkjia.com/PHPjc/739781.html www.bkjia.com true http://www.bkjia.com/PHPjc/739781.html techarticle Copy the code code as follows: Public function downloads ($name) {$name _tmp = explode ("_", $name); $type = $name _tmp[0]; $file _time = Explod E (".", $name _tmp[3]); $file _time = $file _time[0 ...

  • 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.