PHP implementation file Download Simple example

Source: Internet
Author: User
Tags exit explode fread php code ranges

  This article mainly introduces the PHP implementation file download a simple example, the need for friends can refer to the following

The code is as follows: Public function downloads ($name) {$name _tmp = explode ("_", $name); $type = $name _tmp[0]; $file _time = Explode (".", $n AME_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   General implementation download is the call URL to download, but encountered IE can identify open files can not be used in this way, such as downloading a picture, HTML page, etc., then need to program to achieve, The following PHP code can be resolved:   code is as follows: if (Empty ($_get[' FileName ')) | | | empty ($_get[' Filedir ') | | empty ($_get[' Fileid '])) {    EchO ' <script> alert ("Illegal connection!"); Location.replace ("index.php") </script> '; Exit (); } $file _name=$_get[' FileName ']; $file _dir=$_get[' Filedir ']; $FileId =$_get[' Fileid ']; $file _dir = $file _dir. " /"; If   (!file_exists ($file _dir. $file))   { //check file exists     echo   "File not found";     Exit      }   else   {  $file = fopen ($file _dir. $file _name, "R");//Open File//input file Tag Header ("Co Ntent-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 content echo fread ($file, FileSize ($file _dir. $file _name)); Fclose ($file); Exit (); }?>  

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.