Use header definition as file and then ReadFile download

Source: Internet
Author: User
Tags dbx explode fdf gcd pkcs12 pkcs7 readfile rfc822

  Sometimes in order to hide the real download address, we defined the header as a file and then readfile the download, but this will increase the burden on the server, generally do not recommend download a large number of files

The code is as follows: <?php    function SendFile ($fileName, $fancyName = ', $forceDownload = true, $speedLimit = 0, $conten Ttype = ')   {      if (!is_readable ($fileName))       {        &NBS P Header ("http/1.1 404 Not Found");          return false;     }    &NBSP ; $fileStat = stat ($fileName);      $lastModified = $fileStat [' mtime '];        $MD 5 = MD5 ($fileStat [' Mtime ']. " ='. $fileStat [' ino ']. ' ='. $fileStat [' size ']);      $etag = ' "'. $MD 5. '-' . CRC32 ($MD 5). ' "';      header (' last-modified: '. Gmdate ("D, D M Y h:i:s", $lastModified). ' GMT ');      Header ("ETag: $etag");        if (isset $_server[' http_if_modified_ SINCE ']) && strtotime ($_server[' http_if_modified_since ']) >= $lastModified)       {          Header ("http/1.1 304 not Modified ");          return true;     }      if (isset ' Http_if_unmodified_since ']) && strtotime ($_server[' http_if_unmodified_since ']) < $lastModified)       {          Header ("http/1.1 304 Not Modified");          R Eturn true;     }      if (isset ($_server[' Http_if_none_match ')) &&  $_ server[' http_if_none_match '] = = $etag)       {          Header ("http/1.1 304 not Mo Dified ");          return true;     }      if ($fancyName = =") &n Bsp     {          $fancyName = basename ($fileName);     }    &NB Sp   if ($contentType = = ")       {          $contentType = ' application/octet-s Tream ';     }&NBsp     $fileSize = $fileStat [' Size '];           $contentLength = $fileSize;      $isPartial = false;      if (Isset ($_server[' Http_range '))       {          if (Preg_match ('/^bytes= (d*)- (d*) $/', $_server[' Http_range '], $matches))           {           & nbsp     $startPos = $matches [1];              $endPos = $matches [2];              if ($startPos = = ' && $endPos = = ')               {                  return false;          &N Bsp  }                if ($startPos = = ")           &NB Sp   {                  $startPos $fileSize-$endPos;                  $endPos = $fileSize -1;             }              else if ($endPos = = ")               {                  $endPos = $fileSize-1 ;             }              $startPos = $startPos < 0? 0: $startPos;              $endPos = $endPos > $fileSize-1? $fileSize-1: $endPos;              $length = $endPos-$startPos + 1;    & nbsp         if ($length < 0)               {      &NBS P           return false;             }      &NB Sp       $ContentLength = $length;              $isPartial = true;        &NB Sp }     }       //Send headers      if ($isPartial)       {          header (' http/1.1 206 Partial Content ');          header ("Con Tent-range:bytes $startPos-$endPos/$fileSize ");       }      else     {&nbs P         Header ("http/1.1 OK");          $startPos = 0;    &NBSP ;     $endPos = $contentLength -1;     }      header (' Pragma:cache ');  &nbsp ;   Header (' Cache-control:public, must-revalidate, max-age=0 ');      header (' accept-ranges:bytes ') ;      Header (' Content-type: $contentType);      Header (' Content-length: '. $contentLen GTH);        if ($forceDownload)       {          header (' Content-disposition:att Achment; Filename= "'. Rawurlencode ($fancyName). //Chinese characters automatically convert to URL code     header (' content-disposition:attachment; Filename= "'. $fancyName. ' "');     }      header (" Content-transfer-encoding:binary ");        $ buffersize = 2048;      if ($speedLimit!= 0)       {          $pack Ettime = Floor ($bufferSize * 1000000/$speedLimit);     }      $bytesSent = 0;  &nbsp ;   $FP = fopen ($fileName, "RB");      fseek ($fp, $startPos);     //fpassthru ($FP); &nbs P       while ($bytesSent < $contentLength &&!feof ($fp) && connection_status () = = 0) &nbs P     {          if ($speedLimit!= 0)           {              list ($usec, $sec) = Explode ("", Microtime ());               $outputTimeStart = ((float) $usec + (float) $sec);      &NBS P  }          $readBufferSize = $contentLength-$bytesSent < $bufferSize? $contentLength-$bytesSent: $bufferSize;          $buffer = Fread ($fp, $readBufferSize);                 Echo $buffer;          Ob_flush ();          flush ();          $bytesSent + $readBufferSize;            if ($speedLimit!= 0)           {            &N Bsp List ($usec, $sec) = Explode ("", Microtime ());               $outputTimeEnd = ((float) $usec + (float) $sec);                $useTime = ((float) $outputTimeEnd-(float) $outputTimeStart) * 1000000;              $sleepTime = round ($packetTime-$useTime);              if ($sleepTime > 0) & nbsp             {                  Usleep ($sleepTim e);             }         }     }  &nbs P     return true; }   ?>       attached server response HTTP type contenttype Daquan: ". *" = "application/ Octet-stream "". 001 "=" application/x-001 "". "=" application/x-301 "". 323 "=" text/h323 "". 906 "=" application/x-906 "" .907 "=" drawing/907 "". A11 "=" application/x-a11 "". ACP "=" AUDIO/X-MEI-AAC "". Ai "=" application/postscript "". AIF "=" Audio/aiff "". Aifc = "Audio/aiff" ". Aiff" = "Audio/aiff" ". Anv" = "Application/x-anv" ". Asa" = "Text/asa" ". asf" = "video/ X-MS-ASF "". asp "=" text/asp "". asx "=" video/X-ms-asf "". Au "=" audio/basic "". avi "=" Video/avi "". Awf "=" Application/vnd.adobe.workflow "". Biz "=" Text/xml "". bmp "=" Application/x-bmp "". Bot = "Application/x-bot" ". c4t" = "application/x-c4t" ". C90" = "Application/x-c90" ". Cal" = " Application/x-cals "". Cat "=" Application/vnd.ms-pki.seccat "". CDF "=" APPLICATION/X-NETCDF "". Cdr "=" application/ X-cdr "". Cel = "Application/x-cel" ". cer" = "Application/x-x509-ca-cert" ". Cg4" = "Application/x-g4" ". CGM" = " APPLICATION/X-CGM "". cit = "application/x-cit" ". Class" = "java/*" ". CML" = "Text/xml" ". cmp" = "application/x-cmp" ". CMX" = "APPLICATION/X-CMX" ". Cot" = "Application/x-cot" ". CRL" = "application/pkix-crl" ". Crt" = "Application/x-x509-ca-cert" ". CSI" = "Application/x-csi" ". css" = "text/css" ". Cut" = "Application/x-cut" ". dbf" = "application/x-dbf" ". dbm" = " APPLICATION/X-DBM "". dbx = "Application/x-dbx" ". DCD" = "Text/xml" ". Dcx" = "Application/x-dcx" ". Der" = "application/ X-x509-ca-cert "". DGN = "Application/x-dgn" ". Dib" = "Application/x-dib" ". dll" = "Application/x-msdownload" ". Doc" = " AppliCation/msword "". Dot "=" Application/msword "". Drw "=" APPLICATION/X-DRW "". DTD "=" Text/xml "". DWF "=" MODEL/VND.DWF "". DWF "=" APPLICATION/X-DWF "". dwg "=" application/x-dwg "". Dxb "=" APPLICATION/X-DXB "". DXF "=" application/x-dxf "". Edn "=" Application/vnd.adobe.edn "". EMF = "APPLICATION/X-EMF" ". eml" = "message/rfc822" ". Ent" = "Text/xml" ". Epi" = " Application/x-epi "". EPS = "Application/x-ps" ". EPs" = "Application/postscript" ". etd" = "application/x-ebx" ". exe" = " Application/x-msdownload "". Fax = "Image/fax" ". Fdf" = "Application/vnd.fdf" ". Fif" = "Application/fractals" ". Fo" = " Text/xml "". frm = "Application/x-frm" ". G4" = "Application/x-g4" ". GBR" = "APPLICATION/X-GBR" ". gcd" = "application/x-gcd "". gif = "Image/gif" ". Gl2" = "Application/x-gl2" ". Gp4" = "Application/x-gp4" ". Hgl" = "Application/x-hgl" ". HMR" = " APPLICATION/X-HMR "". Hpg = "APPLICATION/X-HPGL" ". HPL" = "APPLICATION/X-HPL" ". hqx" = "application/mac-binhex40" ". HRF" = "APPLICATION/X-HRF" ". HTA" = "Application/hta" ". HTC" = "text/x-component" ". htm" = "text/html" ". html" = "text/htmL "". Htt "=" text/webviewhtml "". htx "=" text/html "". ICB "=" APPLICATION/X-ICB "". ico "=" Image/x-icon "". ico "=" Application/x-ico "". IFF "=" Application/x-iff "". Ig4 "=" Application/x-g4 "". IGs "=" Application/x-igs "". III "=" Application/x-iphone "". img "=" application/x-img "". Ins "=" Application/x-internet-signup "". ISP "=" application/ X-internet-signup "". IVF "=" VIDEO/X-IVF "". Java "=" java/* "". Jfif "=" Image/jpeg "". Jpe "=" Image/jpeg "". Jpe "=" Application/x-jpe "". jpeg "=" Image/jpeg "". jpg = "image/jpeg" ". jpg" = "application/x-jpg" ". js" = "Application/x-javascript" ". jsp" = "text/html". La1 "=" Audio/x-liquid-file "". Lar "=" Application/x-laplayer-reg "". Latex "=" Application/x-latex "". LAVs "=" audio/ X-liquid-secure "". LBM "=" APPLICATION/X-LBM "". Lmsff "=" Audio/x-la-lms "". ls "=" application/x-javascript "". Ltr "=" Application/x-ltr "". m1v = "Video/x-mpeg" ". m2v" = "Video/x-mpeg" ". m3u" = "Audio/mpegurl" ". m4e" = "VIDEO/MPEG4" ". Mac" = " Application/x-mac "". Man "=" Application/x-troff-man "". Math "=" Text/xml "". mdb "=" application/msaccesS "". mdb = "Application/x-mdb" ". MFP" = "Application/x-shockwave-flash" ". mht" = "message/rfc822" ". mhtml" = "message/ rfc822 "". Mi "=" Application/x-mi "". Mid "=" Audio/mid "". Midi "=" Audio/mid "". Mil "=" Application/x-mil "". MML "=" text/xml "". MND "=" Audio/x-musicnet-download "". MNs "=" Audio/x-musicnet-stream "". Mocha "=" Application/x-javascript "". Movie " = "Video/x-sgi-movie" ". Mp1" = "Audio/mp1" ". Mp2" = "AUDIO/MP2" ". mp2v" = "Video/mpeg" ". mp3" = "Audio/mp3" ". mp4" = "video/ MPEG4 "". MPa = "Video/x-mpg" ". MPD" = "Application/vnd.ms-project" ". Mpe" = "Video/x-mpeg" ". Mpeg" = "Video/mpg" ". mpg" = " Video/mpg "". MPGA = "Audio/rn-mpeg" ". MPP" = "Application/vnd.ms-project" ". MPs" = "video/x-mpeg" ". MPT" = "application/ Vnd.ms-project "". MPV "=" Video/mpg "". Mpv2 "=" Video/mpeg "". MPW "=" Application/vnd.ms-project "". MPX "=" application/ Vnd.ms-project "". MTx "=" Text/xml "". MXP "=" APPLICATION/X-MMXP "". Net "=" Image/pnetvue "". NRF "=" APPLICATION/X-NRF "". NWS "=" message/rfc822 "". odc "=" text/x-ms-odc "". Out "=" Application/x-out "". P10 "=" application/pkCs10 "". P12 = "Application/x-pkcs12" ". p7b" = "Application/x-pkcs7-certificates" ". p7c" = "Application/pkcs7-mime" ". p7m "=" Application/pkcs7-mime "". p7r "=" Application/x-pkcs7-certreqresp "". P7s "=" Application/pkcs7-signature "". Pc5 "=" "Application/x-pc5" ". PCI" = "Application/x-pci" ". PCL" = "APPLICATION/X-PCL" ". pcx" = "application/x-pcx" ". pdf" = " Application/pdf "". pdf = "Application/pdf" ". Pdx" = "Application/vnd.adobe.pdx" ". pfx" = "application/x-pkcs12" ". PGL" = "APPLICATION/X-PGL" ". Pic" = "Application/x-pic" ". Pko" = "Application/vnd.ms-pki.pko" ". pl" = "application/x-perl" ". PLG "=" text/html "". Pls "=" audio/scpls "". Plt "=" application/x-plt "". png "=" image/png "". png "=" application/x-png ". Pot "=" application/vnd.ms-powerpoint "". PPA "=" application/vnd.ms-powerpoint "". ppm "=" application/x-ppm "". pps "=" Application/vnd.ms-powerpoint "". ppt = "Application/vnd.ms-powerpoint" ". ppt" = "application/x-ppt" ". PR" = " APPLICATION/X-PR "". PRF "=" Application/pics-rules "". prn "=" application/x-prn "". Prt "=" application/x-prt "". PS "="Application/x-ps" ". PS" = "Application/postscript" ". Ptn" = "Application/x-ptn" ". Pwz" = "application/ Vnd.ms-powerpoint "". R3T = "Text/vnd.rn-realtext3d" ". Ra" = "Audio/vnd.rn-realaudio" ". Ram" = "Audio/x-pn-realaudio". Ras "=" Application/x-ras "". Rat "=" Application/rat-file "". RDF "=" Text/xml "". Rec "=" application/vnd.rn-recording "". Red "=" application/x-red "". RGB "=" Application/x-rgb "". RJs "=" Application/vnd.rn-realsystem-rjs "". Rjt "=" Application/vnd.rn-realsystem-rjt "". RLC "=" APPLICATION/X-RLC "". Rle "=" Application/x-rle "". Rm "=" application/ Vnd.rn-realmedia "". RMF = "APPLICATION/VND.ADOBE.RMF" ". Rmi" = "Audio/mid" ". Rmj" = "application/vnd.rn-realsystem-rmj "". Rmm "=" Audio/x-pn-realaudio "". RMP "=" Application/vnd.rn-rn_music_package "". RMS "=" application/ Vnd.rn-realmedia-secure "". rmvb "=" APPLICATION/VND.RN-REALMEDIA-VBR "". Rmx "=" APPLICATION/VND.RN-REALSYSTEM-RMX "". Rnx "=" Application/vnd.rn-realplayer "". RP "=" Image/vnd.rn-realpix "". RPM "=" Audio/x-pn-realaudio-plugin "". Rsml "=" APPLICATION/VND.RN-RSML "". RT "= "Text/vnd.rn-realtext" ". rtf" = "Application/msword" ". rtf" = "Application/x-rtf" ". RV" = "Video/vnd.rn-realvideo". Sam "=" Application/x-sam "". Sat "=" Application/x-sat "". SDP "=" APPLICATION/SDP "". Sdw "=" Application/x-sdw "". Sit "=" Application/x-stuffit "". SLB = "APPLICATION/X-SLB" ". SLD" = "APPLICATION/X-SLD" ". SLK" = "drawing/x-slk" ". Smi" = " Application/smil "". Smil "=" Application/smil "". SMK "=" APPLICATION/X-SMK "". snd "=" Audio/basic "". Sol "=" Text/plain "". Sor "=" Text/plain "". SPC "=" application/x-pkcs7-certificates "". SPL "=" Application/futuresplash "". spp "=" Text/xml "". SSM "=" Application/streamingmedia "". SST "=" Application/vnd.ms-pki.certstore "" ". StL" = "Application/vnd.ms-pki.stl" " . stm "=" text/html "". Sty "=" Application/x-sty "". svg "=" text/xml "". swf "=" Application/x-shockwave-flash "". TDF "=" Application/x-tdf "". TG4 "=" APPLICATION/X-TG4 "". TGA "=" application/x-tga "". tif "=" Image/tiff "" ". tif" = "application/ X-tif "". Tiff = "Image/tiff" ". TLD" = "Text/xml" ". Top" = "drawing/x-top" ". Torrent" = "Application/x-bittorreNT "". TSD = "Text/xml" ". txt" = "Text/plain" ". UIn" = "Application/x-icq" ". Uls" = "Text/iuls" ". vcf" = "Text/x-vcard" ". Vda" = "Application/x-vda" ". vdx" = "Application/vnd.visio" ". VML" = "Text/xml" ". VPg" = "application/x-vpeg005" ". Vsd" = " Application/vnd.visio "". Vsd "=" Application/x-vsd "". vss "=" Application/vnd.visio "". vst "=" Application/vnd.visio "". VST "=" Application/x-vst "". Vsw "=" Application/vnd.visio "". Vsx "=" Application/vnd.visio "". vtx "=" application/ Vnd.visio "". Vxml = "Text/xml" ". wav" = "audio/wav" ". Wax" = "audio/x-ms-wax" ". Wb1" = "application/x-wb1" ". Wb2" = " Application/x-wb2 "". Wb3 = "Application/x-wb3" ". Wbmp" = "Image/vnd.wap.wbmp" ". Wiz" = "Application/msword" ". Wk3" = " Application/x-wk3 "". Wk4 = "Application/x-wk4" ". Wkq" = "APPLICATION/X-WKQ" ". Wks" = "Application/x-wks" ". Wm" = "video/ X-MS-WM "". wma = "audio/x-ms-wma" ". WMD" = "APPLICATION/X-MS-WMD" ". Wmf" = "application/x-wmf" ". WML" = "text/vnd.wap.wml ". wmv" = "video/x-ms-wmv" ". Wmx" = "video/x-ms-wmx" ". wmz" = "Application/x-ms-wmz" ". Wp6" = "APPLICATION/X-WP6" ". wpd "=" APPLICATION/X-WPD "". WPG "=" APPLICATION/X-WPG "". WPL "=" APPLICATION/VND.MS-WPL "". Wq1 "=" application/x-wq1 "". Wr1 "=" Application/x-wr1 "". Wri "=" Application/x-wri "". Wrk "=" Application/x-wrk "". ws "=" Application/x-ws "". Ws2 "=" Application/x-ws "". WSC = "Text/scriptlet" ". wsdl" = "Text/xml" ". wvx" = "video/x-ms-wvx" ". Xdp" = "application/ VND.ADOBE.XDP "". XDR "=" Text/xml "". XfD "=" Application/vnd.adobe.xfd "". Xfdf "=" application/vnd.adobe.xfdf "". xhtml "= "Text/html" ". xls" = "application/vnd.ms-excel" ". xls" = "Application/x-xls" ". xlw" = "application/x-xlw" "". xml "=" text/ XML "". xPL = "Audio/scpls" ". Xq" = "Text/xml" ". Xql" = "Text/xml" ". XQuery" = "Text/xml" ". xsd" = "text/xml" ". xsl" = "text/xml "". xslt = "Text/xml" ". Xwd" = "application/x-xwd" ". X_b" = "Application/x-x_b" ". x_t" = "application/x-x_t"

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.